Explicit move constructor needed in container?

I have a template container class:

template<class Stuff> class Bag{ private: std::vector<Stuff> mData; }; 

I want to make

  void InPlace(Bag<Array>& Left){ Bag<Array> temp; Transform(Left, temp); //fills temp with desirable output Left = std::move(temp); } 

Suppose Array has user-defined movement semantics, but Bag does not. Will mData move or copy in this case?

+5
source share
1 answer

It will be moved, not copied.

I would suggest looking at the following image:


enter image description here


This clearly shows that the compiler implicitly generates a move constructor if the user does not define his / her own:

  • destructor
  • copy constructor
  • copy assignment
  • move destination

Since your class does not have any of these user-defined constructors, the constructor generated by the compiler will be called, this constructor will be moved by mData .

+8
source

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


All Articles