gpt4 book ai didi

c++ - 增量变量是否可在平局调用中重用?

转载 作者:太空狗 更新时间:2023-10-29 21:09:49 25 4
gpt4 key购买 nike

所以我知道重新使用已递增的变量是 undefined behavior in a function call .我的理解是这在构造函数中不是问题。我的问题是关于 tie 奇怪的是,它们介于两者之间。

给定:pair<int, int> func()我能做吗:

tie(*it++, *it) = func();

或者这是未定义的行为?

最佳答案

自 C++17 起,此代码具有未指定的行为。有两种可能的结果:

  • 第一个参数是取消引用原始迭代器的结果,第二个参数是取消引用增量迭代器的结果;或者

  • 第一个参数和第二个参数都是取消引用原始迭代器的结果。

根据 [expr.call]/8 :

[...] The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter. [...]

因此 tie 的第二个参数可能是取消引用增量迭代器或原始迭代器的结果。


在 C++17 之前,情况有点复杂:

  • 如果 ++* 都调用一个函数(例如,当 it 的类型是一个复杂的类时),然后行为是未指定,类似于自 C++17 以来的情况;

  • 否则,行为未定义

根据 N4140(C++14 草案)[expr.call]/8 :

[ Note: The evaluations of the postfix expression and of the arguments are all unsequenced relative to one another. All side effects of argument evaluations are sequenced before the function is entered (see [intro.execution]). — end note ]

因此,该代码是未定义的行为,因为一个参数的评估与另一个参数没有顺序。两个参数的评估可能会重叠,从而导致数据竞争。除非另有说明......

根据 N4140 [intro.execution]/15 :

When calling a function (whether or not the function is inline), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function. [ Note: Value computations and side effects associated with different argument expressions are unsequenced. — end note ] Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.9 Several contexts in C++ cause evaluation of a function call, even though no corresponding function call syntax appears in the translation unit. [ Example: Evaluation of a new expression invokes one or more allocation and constructor functions; see [expr.new]. For another example, invocation of a conversion function ([class.conv.fct]) can arise in contexts in which no function call syntax appears. — end example ] The sequencing constraints on the execution of the called function (as described above) are features of the function calls as evaluated, whatever the syntax of the expression that calls the function might be.

9) In other words, function executions do not interleave with each other.

因此,如果运算符实际上是函数调用,那么行为同样是未指定的。

关于c++ - 增量变量是否可在平局调用中重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56993196/

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