Search code examples
c++c++11templatesperfect-forwardingtemplate-argument-deduction

Is forwarding an rvalue as an rvalue a use case?


I am wondering about forwarding: The standard implements std::forward basically with two overloads:

  1. To forward lvalues as lvalues/rvalues (dep. of T)
template<typename T>
T&& forward(lvalue_Reference v){ 
    return static_cast<T&&>(v); 
};  
  1. To forward rvalues as rvalues
template<typename T>
T&& forward(rvalue_Reference v){  
    // static-assert: T is not an lvalue-Reference
    return static_cast<T&&>(v); 
};  

First case comes to play when

template<typename T>
void foo(T&& a) { doSomething(std::forward<T>(a)); /* a is lvalue -> matches 1. overload */ }

The second case makes sense, but what is an example of triggering it?


Solution

  • I believe this is a duplicate of:

    What is the purpose of std::forward()'s rvalue reference overload?

    Please also read the included paper:

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2951.html