Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are move constructors produced automatically?

I have a big class holding a lot of STL containers.
Will the compiler automatically make a move constructor that will move those containers to the target or I have to make my own?

like image 643
Dani Avatar asked Nov 27 '11 04:11

Dani


People also ask

Are move constructor automatically generated?

If a copy constructor, copy-assignment operator, move constructor, move-assignment operator, or destructor is explicitly declared, then: No move constructor is automatically generated. No move-assignment operator is automatically generated.

Does compiler provide default move constructor?

Compilers provide a default: Constructor (no arguments) unless another constructor with arguments is declared.

In what way a copy constructor is automatically invoked?

The copy constructor is invoked when the new object is initialized with the existing object. The object is passed as an argument to the function. It returns the object.

What does a move constructor do?

A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying.


1 Answers

A move constructor for a class X is implicitly declared as defaulted exactly when

  • X does not have a user-declared copy constructor,
  • X does not have a user-declared copy assignment operator,
  • X does not have a user-declared move assignment operator,
  • X does not have a user-declared destructor, and
  • the move constructor would not be implicitly defined as deleted.

So for example, if your class has a class type data member that does not have a move constructor, your class will not get a move constructor even if it doesn't have any copy/move constructor declared, because the implicitly declared move constructor would be defined as deleted (because of that data member).

like image 123
Johannes Schaub - litb Avatar answered Sep 20 '22 20:09

Johannes Schaub - litb