decltype and pointers

1

I want to know why, when using decltype (* pointer) - using a pointer - it defines the type of the variable as a reference, eg:

int i = 42, *p = &i;
decltype(*p) c = i;

What I want you to understand in my question is this: Now c is a reference (linked to i), but why is it a reference and not an integer plane? I'm reading the book Cpp Primer 5th. Edition (page 110) says this and I do not understand.

    
asked by anonymous 17.07.2018 / 16:58

1 answer

1

The decltype specifier rules are here: link

Your case falls into any expression (third item), because *p is not a id-expression , nor a class member access , but rather any expression.

  
  • If the argument is any other expression of type T , and

         

    a. if the value category of expression is xvalue, then decltype yields T&& ;

         

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

         

    c. if the value category of expression is prvalue, then decltype yields T .

  •   

    The value category of the expression *p ( indirection expression ) is defined as a lvalue :

      

    The following expressions are lvalue expressions:

         
    • ...
    •   
    • *p , the built-in indirection expression;
    •   

    So, the type where decltype(*p) results comes from rule 3.b .: if the value category of expression is lvalue, then decltype yields T& , where T is the type of expression *p , which in this case is int . Substituting, we have int& as the resulting type.

        
    18.07.2018 / 20:54