When should I use forward and move?

后端 未结 4 1800
长情又很酷
长情又很酷 2021-02-05 12:07

I have a code that operates on a vector:

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


        
相关标签:
4条回答
  • 2021-02-05 12:22
    • 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.

    E.g.

    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

    0 讨论(0)
  • 2021-02-05 12:25
    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.

    0 讨论(0)
  • 2021-02-05 12:32

    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.

    0 讨论(0)
  • 2021-02-05 12:43

    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.

    0 讨论(0)
提交回复
热议问题