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?

5
  • What pointer elements? There aren't any. But your code is fine. Commented Mar 16, 2014 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. Commented Mar 16, 2014 at 15:11
  • 1
    You can consider those pointers as an implementation detail. You don't have to worry about them. Commented Mar 16, 2014 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++. Commented Mar 16, 2014 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. Commented Mar 16, 2014 at 15:21

2 Answers 2

2

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

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

Comments

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.

1 Comment

I should point out that Visual Studio 2012 does NOT implicitly define a move constructor! stackoverflow.com/questions/10201659/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.