Home > Blockchain >  Return type of decltype()
Return type of decltype()

Time:06-03

In the below code, why decltype( c) returns int& instead of int whereas decltype(a ) return int.

#include <iostream>
#include <typeinfo>

int main()
{
    int32_t a {};
    decltype(a  ) b; // OK

    int32_t c{};
    decltype(  c) d; // error: 'd' declared as reference but not initialized

    std::cout << "typeid(b) = " << typeid(b).name() << std::endl;
    std::cout << "typeid(d) = " << typeid(d).name() << std::endl;
}

CodePudding user response:

The result of the built-in pre-increment operator is an lvalue referring to the operand (which then holds the value after the increment). This means that, for example, a = 1; is valid. The result of a refers to the variable a, which can be assigned a value 1.

The result of the built-in post-increment operator is a prvalue of the previous value of the operand (it couldn't be an lvalue referring to the operand since post-increment is supposed to give the previous value before the increment). This means that, eg a = 1; is not valid since the result of a is not referring to a, but is just the original value of a, and a value can't be assigned a value.

decltype applied to a prvalue expression gives a non-reference. Applied to an lvalue expression, it gives an lvalue reference to the expression's type. This mimics the return types you would need to use to get the same value categories for the expressions if you overloaded the increment operators.

CodePudding user response:

This is a result of the pre- and postfix operators' descriptions in the Standard.

cppreference has a little table that shows the signature of the two operators, where ut says:

Prefix versions of the built-in operators return references and postfix versions return values...

by definition, the pre-increment operator returns a reference, as if it were declared:

T& T::operator  ();
T& operator  (T& a);

the post-increment operator returns a value:

T T::operator  (int);
T operator  (T& a, int);

To avoid the error try

#include <type_traits>
//   
// ...later...
//
std::remove_reference_t<decltype(  c)> d; 
  • Related