Search code examples
c++decltype

decltype rules define lvalue or prvalue


decltype statement defines the type depends of its parameter :

  • category of expression is lvalue, then decltype yields T&;
  • if the value category of expression is prvalue, then decltype yields T.

What is a difference and how extra parentheses works here?
How double parentheses makes lvalue category?

int a = 0;

decltype (a) b = a;
cout << is_same<decltype(b), int>::value << '\n';
decltype ((a)) c = a;
cout << is_same<decltype(c), int&>::value << '\n';

Solution

  • decltype has some wonky rules to enable you to express exactly what you want.

    If the expression within decltype is the name of a variable/function (aka a id-expression), then the resulting type is the type of that variable/function.

    Otherwise, if the expression is anything else and its an lvalue of type T, the resulting type is T&, or if its an prvalue of type T, the resulting type is T.