简体   繁体   English

数组[i] = i ++是否被C ++标准所涵盖?

[英]Is array[i] = i++ covered by the C++ standard?

I had a person claiming that this line is not covered by the C++ standard: 我有一个人声称这条线不在C ++标准中:

int i(1);
array_of_int[i] = i++;

The person said that it will assign 1 but we cannot know whether it will be in array_of_int[1] or array_of_int[2] although visual studio and most of compilers will be in array_of_int[1] . 这个人说它会分配1但我们不知道它是否会在array_of_int[1]array_of_int[2]尽管visual studio和大多数编译器都在array_of_int[1]

Is he correct ? 他是对的吗?

This is undefined behavior. 这是未定义的行为。 Literally any behavior is legal. 字面上任何行为都是合法的。

The passage that forbids that line of code is this: 禁止这行代码的段落是这样的:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. 在前一个和下一个序列点之间,对象的存储值最多只能通过表达式的计算修改一次。 Furthermore, the prior value shall be read only to determine the value to be stored 此外,先前的值应该只读以确定要存储的值

There is no sequence point between a[i] and i++ and the read to i in a[i] is not for the purpose of determining what value is stored in i by i++ . 之间不存在序列中的点a[i]i++和读取到ia[i]是不用于确定什么值被存储在的目的i通过i++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM