我的代码从不调用move构造函数。为什么?删除move构造函数时会发生错误。
#include <iostream>
#include <string>
class A{
public:
A(){std::cerr<<"A constructor"<<std::endl;}
A(A&& o){std::cerr<<"A move constructor"<<std::endl;}
//A(A&& o) = delete;
A(const A &p2){std::cerr<<"A copy constructor"<<std::endl;};
~A(){std::cerr<<"~A"<<std::endl;}
A& operator=(const A& other)=delete;
A& operator=(A&& other)=delete;
};
A foo(){
A temp;
std::cerr<<&temp<<std::endl;
return temp;
}
int main() {
A temp = foo();
std::cerr << "Hello World!\n" << &temp << std::endl;
}
输出:
构造函数
0x7fff8ed60ebf
Hello World!
0x7fff8ed60ebf
〜A()
0x7fff8ed60ebf