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?

AGML
  • 890
  • 6
  • 18

1 Answers1

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.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • 2
    Where is this nice table from? – zett42 Jun 15 '17 at 17:35
  • 1
    @zett42 I forgot, I just have it laying around. However, I did a quick search and it's from a presentation : https://stackoverflow.com/a/24512883/1870760 – Hatted Rooster Jun 15 '17 at 17:36
  • 4
    The actual presentation : https://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf – Hatted Rooster Jun 15 '17 at 17:37
  • 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? – AGML Jun 15 '17 at 19:34