看下面的代码:
struct node
{
node();
//node(const node&); //#1
//node(node&&); //#2
virtual //#3
~node ();
node*
volatile //#4
next;
};
int main()
{
node m(node()); //#5
node n=node(); //#6
}
Run Code Online (Sandbox Code Playgroud)
使用gcc-4.6.1编译时会产生以下错误:
g++ -g --std=c++0x -c -o node.o node.cc
node.cc: In constructor node::node(node&&):
node.cc:3:8: error: expression node::next has side-effects
node.cc: In function int main():
node.cc:18:14: note: synthesized method node::node(node&&) first required here
Run Code Online (Sandbox Code Playgroud)
据我所知,编译器无法在第6行创建默认移动或复制构造函数,如果我取消注释第1行或第2行它编译正常,这很清楚.代码在没有c ++ 0x选项的情况下编译正常,因此错误与默认移动构造函数有关.
但是,节点类中的内容会阻止创建默认移动构造函数吗?如果我评论任何第3行或第4行(即使析构函数非虚拟或使数据成员非易失性)它再次编译,那么这两者的组合是否使它不能编译?
另一个难题,第5行不会导致编译错误,与第6行有什么不同?它是否特定于gcc?还是gcc-4.6.1?