从 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.
但是,使用下面的代码:
struct S
{
int arr[5];
};
int main()
{
int &&r = S().arr[0];
}
GCC 和 Clang 都提示“右值引用无法绑定(bind)到左值 int”。
我误解了什么?据我了解 S()
是右值,S().arr
是 xvalue,所以 S().arr[0]
应该是 xvalue以及应该能够绑定(bind)到右值引用。
最佳答案
你是对的,因为你引用的原因。 S().arr[0]
是自 DR 1213 起的一个 xvalue ,因此您应该能够将右值引用绑定(bind)到它。
这是 gcc bug 79832 .请注意,clang HEAD 编译此 fine .
关于c++ - xvalue 上的下标表达式的值类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42750701/