1

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?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Gabriel
  • 8,990
  • 6
  • 57
  • 101
  • Well, totally depends on the use case :-P ... – πάντα ῥεῖ Aug 07 '19 at 17:25
  • 1
    The second case is a universal reference and it forwards both: lvalues as lvalues, rvalues as rvalues. – Sam Varshavchik Aug 07 '19 at 17:27
  • 1
    @SamVarshavchik: The second argument is actually: `rvalue_Reference := typename std::remove_reference_t&&` and is not a forwarding-reference (universal reference) as far as I know... so your statement is incorrect. – Gabriel Aug 07 '19 at 17:34
  • 1
    [Possible dupe](https://stackoverflow.com/questions/56416819/why-forwarding-return-value-is-needed/56417076#56417076) (Similar question, but I wrote an answer that answers this question too) – Artyer Aug 07 '19 at 18:13

1 Answers1

1

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

Okan Barut
  • 279
  • 1
  • 7