我知道 k--
等于 k = k - 1
。我想知道 k = k--
会发生什么,我认为这可能会导致无限循环。但实际上,我在 Visual Studio 2017 中编译这段代码,k = k--
的输出与 k = k - 1
相同。
那么 k = k--
的含义是什么?
最佳答案
之前c++17行为未定义,之后,根据 [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-1
和k=k--
保证产生相同的结果。
另见:cppreference
关于c++ - 当我们在 C++ 中使用 k=k-- 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53551523/