为什么这个程序调用复制构造函数而不是移动构造函数?
class Qwe {
public:
int x=0;
Qwe(int x) : x(x){}
Qwe(const Qwe& q) {
cout<<"copy ctor\n";
}
Qwe(Qwe&& q) {
cout<<"move ctor\n";
}
};
Qwe foo(int x) {
Qwe q=42;
Qwe e=32;
cout<<"return!!!\n";
return q.x > x ? q : e;
}
int main(void)
{
Qwe r = foo(50);
}
Run Code Online (Sandbox Code Playgroud)
结果是:
return!!!
copy ctor
Run Code Online (Sandbox Code Playgroud)
return q.x > x ? q : e;用于禁用nrvo.当我把它包起来时std::move,它确实被移动了.但是在"C++之旅"中,作者说当移动c'tor可用时必须调用它.
我做错了什么?