c++arraystypeslanguage-lawyerdecltype

arr[n] type: a reference according to decltype, a plain type according to the standard?


probably stupid but (https://timsong-cpp.github.io/cppwp/n4950/expr.sub#2.sentence-5)

The expression E1[E2] is identical (by definition) to *((E1)+(E2)), except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise

and (https://timsong-cpp.github.io/cppwp/n4950/expr.unary#op-1)

The unary * operator performs indirection. Its operand shall be a prvalue of type “pointer to T”, where T is an object or function type. The operator yields an lvalue of type T denoting the object or function to which the operand points

So I would expect decltype(arr[5])to be int but std::is_same_v<int&,decltype(arr[5])> is true...

Why?

LIVE


Note: there is this very similar question but the accepted answer does not pinpoint the apparent contradiction regarding operator *


Solution

  • By the parts of standard you referenced, the indirection operator returns an lvalue of type T, and it's not an id-expression, so it should fall into section 1.5 of [dcl.type.decltype]/1:

    otherwise, if E is an lvalue, decltype(E) is T&, where T is the type of E;