6

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?

1
  • good catch, fixed Commented Jun 15, 2017 at 17:14

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Where is this nice table from?
@zett42 I forgot, I just have it laying around. However, I did a quick search and it's from a presentation : stackoverflow.com/a/24512883/1870760
This very usefully answers the question I asked, but I was actually interested in the case when Bag does not have an implicitly generated move constructor. Should I ask a new one?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.