编译器用析构函数定义移动构造函数

compiler defined move constructor with destructor

根据 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 什么都不做。