我对“移动”语义有些含糊:我读到的是,如果类定义了自己的复制控制成员之一,则将移动构造函数或赋值隐式定义为已删除的函数。但是我有这段代码:
int main()
{
struct A {
A() = default;
A(const A&) { cout << "A's cpy-ctor\n"; } // this forces move ctor to be defined as a deleted function
//A(A&&) = default;
//A(A&&) = delete; // if uncomment this line then the line below calling std::move will cause an error(referencing a deleted function).
};
A a = std::move(A{}); // move not available then use copy-ctor instead
std::cout << "\ndone\n";
}
如果我取消对第一条注释行的注释,那么就可以了,就像我猜到的那样:因为没有明确定义它,所以使用copy-constructor代替了move-ctor。
但是如果我取消注释第二条注释行,则在调用std :: move时会出现编译时错误,抱怨删除的函数。但是,为什么编译器不直接使用copy-ctor呢?
什么意思是使这个move-ctor无效?它如何影响功能匹配?
非常感谢!
[当您有一个用户声明的复制构造函数时,移动构造函数和移动分配为未声明