c++ - 空表达式的计算结果是否为 NOP?

标签 c++

想知道空表达式的计算结果是否为 NOP 或者它是否依赖于编译器。

// Trivial example
int main()
{
    ;;
}

最佳答案

它依赖于编译器,但可观察到的行为必须是没有任何反应。实际上,我敢肯定大多数编译器都不会为空表达式省略任何代码。

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input.

可观察的行为定义为:

The least requirements on a conforming implementation are:

  • Access to volatile objects are evaluated strictly according to the rules of the abstract machine.
  • At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced.
  • The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.

These collectively are referred to as the observable behavior of the program.

这实际上是实现的唯一要求。它通常被称为“假设”规则 - 只要可观察到的行为符合预期,编译器就可以做任何它喜欢的事情。

就其值(value)而言,这些空表达式被称为空语句:

An expression statement with the expression missing is called a null statement.

如果你真的想要一个NOP,你可以试试:

asm("nop");

然而,这是有条件支持的,其行为是实现定义的。

关于c++ - 空表达式的计算结果是否为 NOP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718562/

相关文章:

c++ - 数组引用速度与按位运算符

c++ - 将 'typedef' 从基类传播到 'template' 的派生类

c++ - 为什么在 Visual Studio 中单词 `false` 写成蓝色,而单词 `FALSE` 写成紫色?

c++ - std::function 成员可以访问其他成员吗?

c++ - 逻辑与,或 : Is left-to-right evaluation guaranteed?

c++ - C++ 中默认初始化的原始类型是什么?

c++ - 最佳可行的转换函数

c++ - C++ 中的对象数组

c++ - 设备驱动程序库

c++ - 返回值优化的魔力是什么?