c++ - 当我们在 C++ 中使用 k=k-- 时会发生什么?

标签 c++

我知道 k-- 等于 k = k - 1。我想知道 k = k-- 会发生什么,我认为这可能会导致无限循环。但实际上,我在 Visual Studio 2017 中编译这段代码,k = k-- 的输出与 k = k - 1 相同。

那么 k = k-- 的含义是什么?

最佳答案

之前行为未定义,之后,根据 [expr.ass]/1 没问题:

[...] the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. The right operand is sequenced before the left operand

and sequenced before 表示根据 [intro.execution]/8

[...] An expression X is said to be sequenced before an expression Y if every value computation and every side effect associated with the expression X is sequenced before every value computation and every side effect associated with the expression Y.

总之,k=k-1k=k-- 保证产生相同的结果。

另见:cppreference

关于c++ - 当我们在 C++ 中使用 k=k-- 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53551523/

相关文章:

c++ - 如何在 Visual Studio 2010 中的 C++ googletest 项目中设置代码覆盖率

c++ - 添加图标时QMenu项目文本消失

c++ - 无法在 Xcode 上启动 SFML 应用程序

c++ - 将开源 C++ 代码库合并到 Xcode 5.1 中

c++ - 在 AIX 上使用 pthread 进行静态链接

c++ - 依赖倒置原理 : trying to understand

c++ - 用于 C++ 的通用 WebService (SOAP) 客户端库

c++ - 从 bmp(或其他图像类型)中提取第 "n"个图像

c++ - 在 C++ 中解析字符串中的数字

c++ - 更改 QHeaderView 空间缓冲区