C++ 为什么c++;在以下情况下生成构造函数?
我有一个甲级C++ 为什么c++;在以下情况下生成构造函数?,c++,testing,move-semantics,C++,Testing,Move Semantics,我有一个甲级 struct A{ A(){} A(int x): d(x) {} A(const A& a): d(a.d) { std::cout << "copy construction" << std::endl; } A(A&& a): d(a.d){ std::cout << "move construction" << std::en
struct A{
A(){}
A(int x): d(x) {}
A(const A& a): d(a.d) {
std::cout << "copy construction" << std::endl;
}
A(A&& a): d(a.d){
std::cout << "move construction" << std::endl;
}
A& operator=(const A& a){
std::cout << "copy assignment" << std::endl;
d = a.d;
return *this;
}
A& operator=(A&& a){
std::cout << "move assignment" << std::endl;
d = a.d;
return *this;
}
int d;
};
如果我这样做
A x;
x = func();
输出为预期的“移动分配”
但是如果我像这样构造一个
A x = func();
不打印任何东西,好像C++生成自己的移动构造函数,拒绝使用定义的构造函数。
我正在使用VisualStudio14 我真的很想了解这一点感谢您的解释。构造函数调用被省略
- 使用gcc,您可以使用
-fno elide构造函数禁用它
- msvc没有同等的选项
- 使用gcc,您可以使用
-fno elide构造函数禁用它
- msvc没有同等的选项
A x = func();