引自C++ Primer
如果我们明确要求编译器通过使用生成移动操作
= default,而编译器无法移动所有成员,则移动操作将被定义为已删除如果类有一个成员定义了自己的复制构造函数但没有定义移动构造函数,或者如果类有一个成员没有定义自己的复制操作并且编译器为它定义了移动构造函数,则移动构造函数被定义为已删除无法合成移动构造函数
一些代码似乎违反了这条规则:
#include <utility>
#include <iostream>
struct X {
X() = default;
X(const X&) { std::cout << "X(const X&)" << std::endl; }
int i;
};
struct hasX {
hasX() = default;
hasX(const hasX &) = delete;
hasX(hasX &&) = default;
X mem;
};
int main()
{
hasX hx, hx2 = std::move(hx); //output is X(const X&)
}
Run Code Online (Sandbox Code Playgroud)
X 没有定义移动构造函数,编译器不能为它合成一个。
根据上面的规则,hasX删除 的移动构造函数。
但是,由于hasX删除了复制构造函数,hx2 = std::move(hx)必须调用移动构造函数来输出"X(const …