c++type-deductionforwarding-reference

How are universal references/forwarding references deduced when binding to an rvalue?


There is something about Universal references really confuses me, I know that

T&& + lvalue => T&
T&& + rvalue => T&&

Then I heard Scott Meyers says: there is "special rule for universal references" which is:

T&& + rvalue => T (NOT T&&)

So I did simple test thinking it would clear things up

int i = 32;
auto&& v = std::move(i); // assigning xvalue

// According to Scott: auto&& is URef
// And according to the "rule" v should get deduced to T
// VS2015 tells me this is int&& (NOT int)... huh???

auto&& x = int(8); // assigning prvalue
// same as above ... hmmm

So what am I missing here ... I really need to understand this...

When URef get deduced to to T&& and when it get deduced to T ... I would appreciated examples to help clear things up ... thanks

Note: I already searched SO I found related questions but didn't find specific answer to this question.


Solution

  • You forgot where you started from. The whole ideea was to see to what auto deduces to. It indeed deduces to int. And v is of type auto&&, i.e. int&&