Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the type of a named rvalue reference?

Consider the following code:

int&& x = 42;
static_assert(std::is_same<decltype( x ), int&&>::value, "&&");
static_assert(std::is_same<decltype((x)), int& >::value, "&" );

So, what is the type of x? Is it an int&& or an int&?

(I asked myself this question after reading this answer.)

like image 903
fredoverflow Avatar asked Jul 28 '11 18:07

fredoverflow


1 Answers

The type of x (of the variable) is int&&. So decltype(x) yields int&&. The type of the expression x is int. If the expression is an lvalue, decltype((x)) yields a lvalue reference to the type of the expression. So decltype((x)) yields int&.

like image 139
Johannes Schaub - litb Avatar answered Oct 06 '22 23:10

Johannes Schaub - litb