简体   繁体   中英

When should I use forward and move?

I have a code that operates on a vector:

template<typename T>
void doVector(vector<T>& v, T&& value) {
    //....
    v.push_back(value);
    //...
}

For normal push_back , do I need to use forward(value) , move(value) or just value (according to new C++11) ? and how do they impact the performance?

For example,

v.push_back(forward<T>(value));

The current code will not compile when the second argument is lvalue because T&& will turn out to be X& which means T needs to be X& which in turn means std::vector<T> will become std::vector<X&> which will NOT match the first argument which is std::vector<X> & . Hence, it is an error.

I would use two template parameters:

template<typename T, typename V>
void doVector(vector<T> & v, V && value) 
{
    v.emplace_back(std::forward<V>(value));
}

Since V could a different type from T , so emplace_back makes more sense, because not only it solves the problem, it makes the code more generic. :-)

Now the next improvement : since we're using emplace_back which creates the object of type T from argument value (possibly using constructor), we could take advantage of this fact, and make it variadic function:

template<typename T, typename ...V>
void doVector(vector<T> & v, V && ... value) 
{
    v.emplace_back(std::forward<V>(value)...);
}

That is even more generic, as you could use it as:

struct point
{
    point(int, int) {}
};

std::vector<point> pts;
doVector(pts, 1, 2);

std::vector<int> ints;
doVector(ints, 10);

Hope that helps.

  1. forward(value) is used if you need perfect forwarding meaning, preserving things like l-value, r-value.

  2. forwarding is very useful because it can help you avoid writing multiple overloads for functions where there are different combinations of l-val, r-val and reference arguments

  3. move(value) is actually a type of casting operator that casts an l-value to an r-value

  4. In terms of performances both avoid making extra copies of objects which is the main benefit.

So they really do two different things


When you say normal push_back, I'm not sure what you mean, here are the two signatures.

void push_back( const T& value );
void push_back( T&& value );

the first one you can just pass any normal l-val, but for the second you would have to "move" an l-val or forward an r-val. Keep in mind once you move the l-val you cannot use it

For a bonus here is a resource that seems to explain the concept of r-val-refs and other concepts associated with them very well.

As others have suggested you could also switch to using emplace back since it actually perfect forwards the arguments to the constructor of the objects meaning you can pass it whatever you want.

  • When passing a parameter that's a forwarding reference , always use std::forward .
  • When passing a parameter that's an r-value , use std::move .

Eg

template <typename T>
void funcA(T&& t) {
    funcC(std::forward<T>(t)); // T is deduced and therefore T&& means forward-ref.
}

void funcB(Foo&& f) {
    funcC(std::move(f)); // f is r-value, use move.
}

Here's an excellent video by Scott Meyers explaining forwarding references (which he calls universal references ) and when to use perfect forwarding and/or std::move :

C++ and Beyond 2012: Scott Meyers - Universal References in C++11

Also see this related question for more info about using std::forward : Advantages of using forward

http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Scott-Meyers-Universal-References-in-Cpp11

That video, IMO, has the best explanation of when to use std::forward and when to use std::move. It presents the idea of a Universal Reference which is IMO an extremely useful tool for reasoning about move semantics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM