AGML
AGML

Reputation: 920

Explicit move constructor needed in container?

I have a templated container class:

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

I want to do

  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 move semantics, but Bag does not. Would mData in this case be moved or copied?

Upvotes: 6

Views: 850

Answers (1)

Hatted Rooster
Hatted Rooster

Reputation: 36503

It would 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 as long as the user doesn't define his/her own :

  • destructor
  • copy constructor
  • copy assignment
  • move assignment

Since your class has none of these user defined constructors the compiler generated move constructor will be called, that constructor will move mData.

Upvotes: 9

Related Questions