1

Ok, I will put up the code first, and then ask my question.

#include<vector>
#include<string>

using std::vector;
using std::string;

class MyStringContainer
{
public:
   MyStringContainer(vector<string> strVec): _strVec(strVec){;}
   MyStringContainer(MyStringContainer&& rhs): _strVec(move(rhs._strVec)){;}
private:
   vector<string> _strVec;
}

int main()
{
  vector<string> dummyVec(1000000, "arbitrary string");

  MyStringContainer strCon1(dummyVec);
  MyStringContainer strCon2(move(strCon1));
}

So I just spent some time learning move semantics, and I think I got the basic idea of it concerning how to swap raw and/or smart pointers around and setting the discarded pointers to nullptr. However when dealing with vectors (and all the containers that implement move semantics), I am not 100% sure if my code above will properly "nullptr" the pointer elements of the source vector. Does the implementation of the std::vector class already handle this for me?

LoadRunner
  • 43
  • 4
  • What pointer elements? There aren't any. But your code is fine. – juanchopanza Mar 16 '14 at 15:02
  • @juanchopanza: I thought I read somewhere that the std::vector class does consist of a few pointers (not sure, as I am new to stl/c++11), and that memory is allocated/deallocated dynamically for every push_back()/pop_back() call, which I thought envolved having pointer elements. – LoadRunner Mar 16 '14 at 15:11
  • 1
    You can consider those pointers as an implementation detail. You don't have to worry about them. – juanchopanza Mar 16 '14 at 15:12
  • @juanchopanza: Hey man, thanks. Really appreciate it. :) Just wanted to make sure I am not running into any memory leaks. I really am from the old school c++. – LoadRunner Mar 16 '14 at 15:15
  • 1
    BTW you don't actually need to provide the copy and move-copy constructors. The compiler generated ones will do the right thing. – juanchopanza Mar 16 '14 at 15:21

2 Answers2

2

Implementation of the std::vector handles this for you.

Blaz Bratanic
  • 2,279
  • 12
  • 17
1

The std::vector class will handle this for you. In fact in this case you could use the implicitly generated move constructor. Also if your constructor takes the vector<string> by value, which is fine, you may want to use std::move to move it into the member variable to save a copy:

#include<vector>
#include<string>

class MyStringContainer {
private:
   std::vector<std::string> strVec_;
public:
   MyStringContainer(std::vector<std::string> strVec): strVec_(std::move(strVec)){ }
};

int main()
{
  std::vector<std::string> dummyVec(1000000, "arbitrary string");
  MyStringContainer strCon1(std::move(dummyVec));
  MyStringContainer strCon2(std::move(strCon1));
}

Note I have used std::move to move the dummyVec into the constructor as well to save another copy.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • I should point out that Visual Studio 2012 does NOT implicitly define a move constructor! http://stackoverflow.com/questions/10201659/why-is-this-copy-constructor-called-rather-than-the-move-constructor – Chris Drew Mar 19 '14 at 17:30