具有 void 类型分支的三元运算符

Ternary operator with branches with void type

在 return 无效的函数中使用三元运算符是否安全?像这样:

void foo1(){}
void foo2(){}

//////////////

bool to_use_1 = ....;
to_use_1 ? foo1() : foo2();

编译器可以删除这段代码吗?假设它将这些函数视为纯函数,并执行删除这些调用的积极优化

首先,编译器 will/should 从不在逻辑上“删除”任何具有可观察效果的调用(唯一的例外是 copy elision)。无论优化模式多么激进,这都是不允许的事情。

C++ 标准实际上明确允许条件运算符的操作数的结果为 void,因此这是预期且安全的。但是,它确实必须 both.

[C++14: 5.16/1]: Conditional expressions group right-to-left. The first expression is contextually converted to bool (Clause 4). It is evaluated and if it is true, the result of the conditional expression is the value of the second expression, otherwise that of the third expression. Only one of the second and third expressions is evaluated. Every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second or third expression.

[C++14: 5.16/2]: If either the second or the third operand has type void, one of the following shall hold:

  • The second or the third operand (but not both) is a (possibly parenthesized) throw-expression (15.1); the result is of the type and value category of the other.

  • Both the second and the third operands have type void; the result is of type void and is a prvalue. [ Note: This includes the case where both operands are throw-expressions. —end note ]

从技术上讲,这个措辞(在其关于“值”的讨论中)并没有完全说明确切地第二个和第三个表达式之一被计算,即使这些计算没有“价值”。但从分析的角度来看,这在本质上是无可争辩的。 可能需要在此处进行编辑改进。

请注意 none 这保证您的计算机将 实际上 在执行过程中跳转到 foo1foo2;在你给出的具体例子中,编译器可以立即看到这两个函数都是空的,并优化掉整行代码。但这不会影响程序的语义,也不会特定于条件运算符的使用。