c++ - 条件运算符的值类别

标签 c++ g++ language-lawyer conditional-operator value-categories

考虑以下代码:

int x;
int& f() {
  return x ? x : throw 0;
}

使用 gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04) 我得到以下编译错误:

cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’

请注意,这在 clang 中编译得很好。这是(我认为是)标准中的相关声明:

N4659 [8.16.2.1] (Conditional Operator):
The second or the third operand (but not both) is a (possibly parenthesized) throw-expression (8.17); the result is of the type and value category of the other.

据我所知,x 是一个左值,所以在我看来 clang 是正确的。我错了吗?


如果我不得不猜测的话,会发生左值到右值的转换,因为条件表达式中的两个表达式不具有相同的类型,但是因为第二个是抛出,所以应该抢占此转换。我不熟悉提交错误报告,但也许这会是一个更好的论坛。
以下是一些(可能)更有用的关于条件运算符的问题:
Why does this function return an lvalue reference given rvalue arguments?
Error: lvalue required in this simple C code? (Ternary with assignment?)

最佳答案

clang 在这里是正确的,旧的行为是无条件地将值转换为 prvalue,看起来 gcc 仍在实现。

这是 DR 1560 的主题由 DR 1550 的决议修复. DR 1560 说:

A glvalue appearing as one operand of a conditional-expression in which the other operand is a throw-expression is converted to a prvalue, regardless of how the conditional-expression is used:

If either the second or the third operand has type void, then the lvalue-to-rvalue (7.1 [conv.lval]), array-to-pointer (7.2 [conv.array]), and function-to-pointer (7.3 [conv.func]) standard conversions are performed on the second and third operands, and one of the following shall hold:

  • The second or the third operand (but not both) is a throw-expression (18.1 [except.throw]); the result is of the type of the other and is a prvalue.

这似乎是无端的和令人惊讶的。

DR 1550 更改了 [expr.cond] 中的措辞我们现在拥有的:

The second or the third operand (but not both) is a (possibly parenthesized) throw-expression; the result is of the type and value category of the other. The conditional-expression is a bit-field if that operand is a bit-field.

所以看起来 gcc 正在执行旧行为,而 clang 正在执行 DR。

这是 patch that applied DR 1560 to clang .它添加了以下测试:

namespace DR1560 { // dr1560: 3.5
  void f(bool b, int n) {
    (b ? throw 0 : n) = (b ? n : throw 0) = 0;
  }
  class X { X(const X&); };
  const X &get();
  const X &x = true ? get() : throw 0;
}

哪个on godbolt we can see this fails for gcc由于:

error: lvalue required as left operand of assignment
4 |     (b ? throw 0 : n) = (b ? n : throw 0) = 0;
  |                    

我们有一个 gcc bug report for a very similar issue它具有以下简化的测试用例:

I wanted to bump this bug and supply a simpler test-case:

void blah(int&) {}

int main() {
    int i{};
    blah(true ? i : throw);
}

result with gcc 6.0:

prog.cc: In function 'int main()':
prog.cc:6:15: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
     blah(true ? i : throw 0);
          ~~~~~^~~~~~~~~~~~~

关于c++ - 条件运算符的值类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53328407/

相关文章:

c++ - 将 vector <int>转换为const vector <const int>

c++ - 为什么会出现以下段错误?

C++ 私有(private)函数 - 不在此范围内错误

c++ - gcc 允许未初始化的 constexpr

c++ - C++ 中 if/else 分支的编译时消除

c++ - 条件变量虚假唤醒/循环任务

c++依赖注入(inject)来测试类系统调用的类

c++ - g++ 关于零文字的不一致

c++ - 匿名临时对象和类模板参数推导 - gcc vs clang

c++ - 从 nullptr_t 到 bool : valid or not? 的转换