According to [expr.ref]/(4.2) and the fact that A()
is a prvalue, we conclude that A().a[0]
is an xvalue. See highlighted sentence below.
If E2 is a non-static data member and the type of E1 is “cq1 vq1 X”, and the type of E2 is “cq2 vq2 T”, the expression designates the named member of the object designated by the first expression. If E1 is an lvalue, then E1.E2 is an lvalue; otherwise E1.E2 is an xvalue. Let the notation vq12 stand for the “union” of vq1 and vq2; that is, if vq1 or vq2 is volatile, then vq12 is volatile. Similarly, let the notation cq12 stand for the “union” of cq1 and cq2; that is, if cq1 or cq2 is const, then cq12 is const. If E2 is declared to be a mutable member, then the type of E1.E2 is “vq12 T”. If E2 is not declared to be a mutable member, then the type of E1.E2 is “cq12 vq12 T.
Therefore the snippet below should compile. The code doesn't compile in GCC, neither in VS2017, but it compiles in clang.
#include<iostream>
struct A
{
int a[3];
A(): a{1, 2, 3} {}
};
int main()
{
int &&r = A().a[0];
std::cout << r << '\n';
}
However, the wording in [expr.sub]/1 indicates that a[0]
is an lvalue, irrespective of the value category of A()
, and that seems to be incorrect to me.
A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall be a glvalue of type “array of T” or a prvalue of type “pointer to T” and the other shall be a prvalue of unscoped enumeration or integral type. The result is of type “T”. The type “T” shall be a completely-defined object type.66 The expression E1[E2] is identical (by definition) to *((E1)+(E2)) [ Note: see 8.5.2 and 8.5.6 for details of * and + and 11.3.4 for details of arrays. —end note ] , except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise. The expression E1 is sequenced before the expression E2.