From 5.2.1.1:
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.
However, with the code below:
struct S
{
int arr[5];
};
int main()
{
int &&r = S().arr[0];
}
both GCC and Clang complain about "rvalue reference cannot bind to lvalue int".
What have I misunderstood? As I understand S()
is rvalue, S().arr
is xvalue, so S().arr[0]
should be xvalue as well and should be able to bind to rvalue refs.
You are correct, for the reason you cite. S().arr[0]
is an xvalue as of DR 1213, so you should be able to bind an rvalue reference to it.
This is gcc bug 79832. Note that clang HEAD compiles this fine.