gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:35 24 4
gpt4 key购买 nike

我知道 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com