Conditions automatically generated by default move construction and default move assignment

1. When will the default move constructor be automatically generated?

If you need to use an rvalue to copy-construct a class object or assign a value to a class object, if the class has a customized move constructor/move assignment function, the class's customized move constructor/move assignment will be called. function

In the absence of a custom move constructor/move assignment function, if the class customizes one of the copy constructor/assignment operator or destructor, only the copy constructor/assignment operator will be called (provided that The formal parameter is const T& instead of T&, because const T& can be compatible with rvalue arguments, but T& cannot) and a default move constructor will not be generated. Note that even if there is no custom copy constructor/assignment operator and only a custom destructor, a default move constructor will not be generated. This is because the custom destructor indicates that the class may need to reclaim memory when it is destructed. If a default move constructor is generated, an error may occur (such as an error where the same address is released twice)

  1. No copy constructor declared
  2. No assignment operator declared
  3. No destructor declared

2. When will the default move assignment be automatically generated?

  1. No copy assignment function declared.
  2. No copy constructor declared.
  3. No move constructor declared.
  4. Move assignment functions are not implicitly declared delete.
  5. No destructor declared.

Guess you like

Origin blog.csdn.net/AkieMo/article/details/132126458