【发布时间】:2015-07-04 23:12:41
【问题描述】:
我认为当我删除 B 中的移动构造函数时,以下代码仍然可以正常编译,因为它仍然应该使用复制构造函数来构造 B 对象。为什么编译器现在抱怨。如果没有=delete,它不会调用复制构造函数,因为它不允许提供默认的移动构造函数!)
class B{
public:
B(){}
~B(){}
B & operator=(const B & b){
std::cout << " cannot move -> copy " << std::endl;
return *this;
}
B(const B & v){
std::cout << " cannot move -> copy " << std::endl;
}
// B(B && b) = delete; // uncomment this!
};
int main()
{
B b( B{} );
}
使用 clang 3.6 (Live code) 的编译器输出
main.cpp:27:7: error: call to deleted constructor of 'B'
B b( B{} );
^ ~~~
main.cpp:21:5: note: 'B' has been explicitly marked deleted here
B(B && b) = delete;
^
1 error generated.
【问题讨论】:
-
“函数未定义”和“函数定义为已删除”不是一回事。后者表示该函数正常参与重载决议,如果实际选择则产生错误。
标签: c++ c++11 copy-constructor c++14 move-semantics