Default move constructor vs. Default copy constructor vs. Default assignment operator

后端 未结 3 511
-上瘾入骨i
-上瘾入骨i 2021-02-11 16:30

Why does C++ compiler have more restriction on automatically generated move constructors than on automatically generated copy constructor or assignment operator ?

Automa

3条回答
  •  时光说笑
    2021-02-11 17:13

    I believe backwards compatibility plays a big part here. If the user defines any of the "Rule of three" functions (copy ctor, copy assignment op, dtor), it can be assumed the class does some internal resource management. Implicitly defining a move constructor could suddenly make the class invalid when compiled under C++11.

    Consider this example:

    class Res
    {
      int *data;
    
    public:
      Res() : data(new int) {}
    
      Res(const Res &arg) : data(new int(*arg.data)) {}
    
      ~Res() { delete data; }
    };
    

    Now if a default move constructor was generated for this class, its invocation would lead to a double deletion of data.

    As for the move assignment operator preventing default move constructor definitions: if the move assignment operator does something other than default one, it would most likely be wrong to use the default move constructor. That's just the "Rule of three"/"Rule of five" in effect.

提交回复
热议问题