用memcpy移动构造函数

时间:2013-11-05 12:50:48

标签: c++ c++11 move-constructor

我有一个结构,我想要不可复制,只能移动,但因为它包含很多POD,写移动构造函数会很长,忘记变量将很难调试。例如:

struct myStruct{
    int a,b,c,d;
    double e,f,g,h;
    std::complex<double> value1,value2;

    std::unique_ptr<Calculator> calc;

    myStruct(){}
    myStruct(const myStruct &)=delete;
    myStruct(myStruct && other);
};

这种移动构造函数会出现什么问题:

myStruct::myStruct(myStruct && other){
    std::memcpy(this,&other,sizeof(myStruct));
    other.calc.release();
    calc->rebind(this);
}

我可以面对哪些问题并且定义明确?

2 个答案:

答案 0 :(得分:5)

最小的改变只是将简单的初始化成员组合在一起,因此您可以轻松地memcpy

struct myStruct{
    struct {
        int a,b,c,d;
        double e,f,g,h;
        std::complex<double> value1,value2;
    } pod;

    std::unique_ptr<Calculator> calc;

    myStruct(){}
    myStruct(const myStruct &)=delete;
    myStruct(myStruct && other);
};

myStruct::myStruct(myStruct && other){
    std::memcpy(&pod,&other.pod,sizeof(pod));
    other.calc.release();
    calc->rebind(this);
}

注意std::complex文字类型,放入pod成员应该是安全的。如果您添加类类型的任何其他成员对象,则必须验证自己对memcpy是否安全。


正如Jonathan Wakely指出的那样,更好的实施方式将回避对pod和非pod(或文字和普通初始化)成员的关注。而是根据您是否要复制或移动它们来对成员进行分组:

struct myStruct{
    struct {
        int a,b,c,d;
        double e,f,g,h;
        std::complex<double> value1,value2;
    } val;

    std::unique_ptr<Calculator> calc;

    myStruct(){}
    myStruct(const myStruct &)=delete;
    myStruct(myStruct && other);
};

myStruct::myStruct(myStruct && other)
  : val(other.val)              // copy the value types
  , calc(std::move(other.calc)) // and move the reference types
{
    calc->rebind(this);
}

答案 1 :(得分:4)

您可以使用默认移动Ctor:

myStruct(myStruct&& other) = default;