13

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.)

Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662

1 Answers1

14

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&.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212