根据 cpp 引用 cpp-ref,如果我们有一个用户定义的析构函数,编译器不会生成默认的移动构造函数。
代码片段:
class General
{
public:
~General();
General();
void testInitList();
};
int main(int argc, char **argv) {
General b(std::move(General()));
General g = std::move(b);
g.testInitList();
return 0;
}
代码编译意味着编译器生成了一个默认的移动构造函数。该代码是使用 gcc 版本 5.4.0 编译的。
有人可以解释为什么编译器在这种情况下生成移动构造函数和移动赋值运算符,尽管有一个析构函数?
最好拉胡尔
如果没有移动构造函数或赋值运算符,则不执行移动。 std::move 不执行移动。它只是转换其参数以指示如果可能,可以执行移动。如果不可能,那么就没有移动,使用std::move
也没有任何作用。