根据移动赋值运算符移动构造函数

amc*_*176 3 c++ move c++11

在我们项目的代码库中,我发现了这样的事情:

struct MeshData {
    MeshData() {}
    MeshData(MeshData&& obj) { *this = std::move(obj); }
    MeshData& operator=(MeshData&& obj) {
        if (this != &obj) {
            indexes = std::move(obj.indexes);
        }
        return *this;
    }
    std::vector<int> indexes;
};
Run Code Online (Sandbox Code Playgroud)

在移动分配方面实现移动构造对我来说似乎是一个聪明的想法,减少了代码重复,但在查找信息之后,我没有找到任何关于此的具体建议.

我的问题是:这是反模式还是有任何不应该做的情况?

Tar*_*ama 7

如果您的类型的数据成员没有默认构造函数,则无法执行此操作,如果它们具有昂贵的默认构造函数,则不应执行此操作:

struct NoDefaultCtor {
    NoDefaultCtor() = delete;
    NoDefaultCtor(int){}
};

struct Foo {
    Foo(Foo&& obj) { *this = std::move(obj); }
    Foo& operator=(Foo&& obj) {
        if (this != &obj) {
            thing = std::move(obj.thing);
        }
        return *this;
    }
    NoDefaultCtor thing;
};
Run Code Online (Sandbox Code Playgroud)

给出了这个错误:

<source>: In constructor 'Foo::Foo(Foo&&)':
<source>:10:20: error: use of deleted function 'NoDefaultCtor::NoDefaultCtor()'
     Foo(Foo&& obj) { *this = std::move(obj); }
                    ^
<source>:5:5: note: declared here
     NoDefaultCtor() = delete;
Run Code Online (Sandbox Code Playgroud)

这是因为必须在输入构造函数体之前构造所有数据成员.

但是,最好的建议是遵循零规则,并避免编写这些特殊成员.