c++ - xvalue 上的下标表达式的值类别

标签 c++ c++14 language-lawyer rvalue-reference xvalue

从 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/

相关文章:

eclipse - 在 Eclipse 中启用 C++14 语法检查

c++ - 如何在 std::vector 中查找模式

c++ - 消除c++中菱形继承(钻石问题)访问顶级成员的歧义

c++ - 将此作为参数使用 va_start 宏是否安全?

c++ - 为什么模板参数替换的顺序很重要?

c++ - fork() 与 C++ 中的 std::async

c++ - 在C++中避免for循环。但为什么?

c++ - 放大 QGraphicsScene 有时会使我的绘图消失

c++ - 简单 C++ 中的错误,但无法弄清楚原因

c++ - 我应该在类的析构函数中删除 QPointer 吗?