Explicitly, the default move constructor

According to the C ++ 11 standard, a default move constructor is only generated if:

  • X does not have a user-declared copy constructor and
  • X does not have a user-declared copy destination operator,
  • X does not have a user-declared move destination operator,
  • X does not have a user-declared destructor and
  • the move constructor will not be implicitly defined as remote.

Can I still use it explicitly? Seems to work correctly in clang. For example, for example:

class MyClass { private: std::vector<int> ints; public: MyClass(MyClass const& other) : ints(other.ints) {} MyClass(MyClass&& other) = default; }; 
+6
source share
2 answers

The motivation for this rule is that if the default copy constructor does not work for your class, the likelihood that the default move constructor will not work (rule 5 or something else that we do in C ++ 11). So yes, you can clearly by default, in your honor to the programmer, that he will work.

In your code example, you can remove the copy constructor instead, as it does the same thing as the default.

+10
source

Yes, you can always explicitly call default generation for functions that can be automatically generated using = default . This is for syntax.

+3
source

Source: https://habr.com/ru/post/917910/


All Articles