c++ - 后增量运算符行为 w.r.t 逗号运算符?

标签 c++ comma post-increment

在以下代码中:

int main() {
   int i, j;

   j = 10;
   i = (j++, j+100, 999+j);

   cout << i;

   return 0;
}

输出为 1010 .

但是不应该是 1009,如 ++应该在使用整个表达式后完成?

最佳答案

逗号运算符是一个序列点:例如,正如在 C++17 标准中所说,

Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression.



因此,++的效果运算符保证在 999+j 之前发生被评估。

关于c++ - 后增量运算符行为 w.r.t 逗号运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61001093/

相关文章:

c++ - 为什么我在组合增量运算符时得到 "error: expression is not assignable"

C++ 模板错误:从 'void TestClass::testMethod(T, std::map) [with T = SomeClass] 实例化

c++ - 'using' 和 'using namespace' 之间的区别

java - 在逗号Java之后热将小数舍入到下一个5的乘法

mysql - MySql选择所有记录,并用逗号分隔符显示在单个列中

c# - 将 x 后递增 n (n != 1)

c++ - C++ 中符合标准的最小对象大小?

c++ - 通过引用返回已销毁的局部变量的成员

PHP:将逗号写入 csv 文件

Java前后递增