我创建一个类A,并覆盖operator +和构造函数A(A&&)
,我想知道是否调用
A a, b;
A c = a + b;
我的构造函数
A(A &&)
被调用了吗?我尝试了一些代码,但结果很困惑
//try2.cpp
#include <iostream>
#include <string>
using namespace std;
class A {
public:
string s;
A(A &&a) { s=std::move(a.s); cout<<"A(A &&a) called"<<endl;}
A() {}
};
A operator+(const A &a, const A &b) {
A c;
c.s=a.s+b.s;
return c;
}
int main() {
A a,b;
a.s = "123";
b.s = "456";
A c = a+b;
cout<<c.s<<endl;
return 0;
}
我使用了gcc 7.0:
g++ -std=c++1y try2.cpp
输出为
123456
,因此不调用A(A &&a)
。但是后来我改变了
A(A &&a) = delete;
然后编译器抛出错误:
try2.cpp: In function ‘A operator+(const A&, const A&)’:
try2.cpp:14:10: error: use of deleted function ‘A::A(A&&)’
return c;
^
try2.cpp:7:3: note: declared here
A(A &&a) = delete;
^
try2.cpp: In function ‘int main()’:
try2.cpp:21:11: error: use of deleted function ‘A::A(A&&)’
A c = a+b;
^
try2.cpp:7:3: note: declared here
A(A &&a) = delete;
构造函数
A(A &&a)
是必需的。但是为什么以前的代码没有调用它? 最佳答案
Named Return Value Optimization。
编译器意识到,与其(在A
范围内创建operator+
并将其从operator+
中移出并移入main
中),它(基本上)可以仅在main
中构造它,并使operator+
对main
中的值起作用。
为了使您的程序有效,您需要一些构造函数来复制该值-但如果编译器意识到可以优化某些副本/移动,则无需使用它。