2

If in my code, I have declared a variable (for example string) and want to pass to some function so my vector would add that value. Do I always need to do this(using the std::move), considering I do not need that passed variable anymore?

Void add(string s){
    my_vector.push_back(move(s));
}

I do know that I can simply pass an rvalue but consider if I needed to declare the string first (making it an rvalue) and then adding it to the vector.

Because I don’t want to copy the variable and then push back to my vector

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 2
    Your question is unclear. What do you think the code you posted does? What do you think it does if you leave out the `move`? What do you want it to do? Your formulation indicates that you are confused about move semantics in C++, but the way the question is asked, the only thing I can do is link you to a tutorial that explains them from the ground up. – Sebastian Redl Jan 02 '18 at 09:15
  • 1
    Btw. please, consider, `std::vector::emplace_back()` as alternative to `std::vector::push_back()` which allows "in-place" construction to prevent one copy step. – Scheff's Cat Jan 02 '18 at 09:20
  • For better understanding of this, I experimented with vector of an own class where I deleted copy constructor and copy assignment but had to add a move constructor and move assignment. – Scheff's Cat Jan 02 '18 at 09:25
  • @Scheff Don't use `emplace_back` if you have a value of the right type already. Only use it when you want to construct a value in-place using constructor arguments. But not when doing so would use the copy or move constructor. `push_back` is the right function for those cases. – Sebastian Redl Jan 02 '18 at 10:15
  • "*I don’t want to copy the variable and then push back to my vector*" - but that is exactly what your example is doing. `string s` is passed by value, so it makes a copy of whatever is passed to it. Then you are `move`'ing that copy into the vector – Remy Lebeau Jan 02 '18 at 19:53

2 Answers2

1

If you want the string to be moved, you have to say so. Otherwise it will be copied.

But unless this is a hot spot in your application, the difference might be small enough not to be noticed.

For a std::string in particular, the major implementations use a Small String Optimization, where short strings (typically 10-20 bytes) will be stored inside the string object to save on heap allocations. In that case moving and copying will do the same thing for the short strings.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

Depending on what you intend to do with your add method and how you plan to use it, you should consider using a universal reference: you declare the s argument as a rvalue reference and then:

  • if the calling argument is a lvalue, s will be a lvalue reference;
  • if the calling argument is a rvalue, s will be a rvalue reference.

Hence, the following code will benefit from move semantic optimisation whenever possible:

void add(string && s) {
    my_vector.push_back(s);
}
Xavier Lamorlette
  • 1,152
  • 1
  • 12
  • 20