gpt4 book ai didi

c++ - C++11 之前的初始化列表中同一变量的多个突变是否未定义行为

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

考虑以下代码:

int main()
{
int count = 0 ;
int arrInt[2] = { count++, count++ } ;

return 0 ;
}

如果我们使用 clang -std=c++03 编译代码,它会产生以下警告( live example ):

warning: multiple unsequenced modifications to 'count' [-Wunsequenced]
int arrInt[2] = { count++, count++ } ;
^ ~~

我不是提倡这样的代码,但类似的代码出现在另一个问题中,并且对于它是否是 定义 是否根据 C++11 之前的标准存在分歧。在 C++11 中,此行为是根据 Are multiple mutations within initializer lists undefined behavior 明确定义的行为事实上,如果我使用 -std=c++11 那么警告就会消失。

如果我们看一下 C++11 draft standard它没有涵盖initializer-list 的相同语言,所以看起来我们只剩下Chapter 5 Expressions paragraph 4 上面写着:

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.57) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

为了未定义,我们似乎必须将count++, count++解释为表达式,因此每个count++ 作为子表达式,所以这段代码未定义 C++11 之前?

最佳答案

代码不是未定义 C++11 但评估顺序是未指定。如果我们看一下标准草案 1.9 程序执行12 说:

A full-expression is an expression that is not a subexpression of another expression. [...]

15 段说:

There is a sequence point at the completion of evaluation of each full-expression12).

那么问题是 count++, count++ 是否是一个完整表达式 并且每个 count++ 是否是一个子表达式还是每个 count++ 都是自己的完整表达式,因此每个表达式后都有 序列点?如果我们从 8.5 Initializers 部分查看此初始化的语法:

initializer-clause:
assignment-expression
{ initializer-list ,opt }
{ }
initializer-list:
initializer-clause
initializer-list , initializer-clause

我们唯一的表达式是一个赋值表达式分隔组件是初始化列表的一部分 并且不是表达式的一部分,因此每个 count++ 都是一个完整表达式,并且有一个序列点 在每一个之后。

此解释由以下 gcc 确认 bug report ,它与我的代码非常相似(在发现此错误报告之前我想出了示例方法):

int count = 23;
int foo[] = { count++, count++, count++ };

最终为 defect report 430 ,我将引用:

[...]I believe the standard is clear that each initializer expression in the above is a full-expression (1.9 [intro.execution]/12-13; see also issue 392) and therefore there is a sequence point after each expression (1.9 [intro.execution]/16). I agree that the standard does not seem to dictate the order in which the expressions are evaluated, and perhaps it should. Does anyone know of a compiler that would not evaluate the expressions left to right?

关于c++ - C++11 之前的初始化列表中同一变量的多个突变是否未定义行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19881803/

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