简体   繁体   English

我将用++ + i ++在C ++ 17中评估什么?

[英]What will i++ + i++ evaluate to in C++17?

Looks like we're getting a whole new breed of "interview questions" for C++ (I hope not, actually). 看起来我们正在为C ++获得一个全新的“面试问题”(我希望不是,实际上)。

It is known to be undefined behavior prior to C++17, but will it be well-defined from C++17 onward? 众所周知,在C ++ 17之前,它是未定义的行为 ,但它是否会从C ++ 17开始明确定义

Since at the moment there doesn't seem to be a compiler that implements this C++17 modification, can anyone explain what will, according to expression evaluation rules, the value of x be in the following code? 由于目前似乎没有一个编译器实现了这个C ++ 17的修改,任何人都可以解释一下,根据表达式评估规则, x的值在下面的代码中?

int i = 0;
int x = i++ + i++;

Alisdair Meredith mentions this example here in his CppCon 2016 talk, but it's not entirely clear to me what the final value of x will be (although it seems what he's saying is that it'll be at least 1). 阿利斯代尔梅雷迪思提到这个例子在这里他CppCon 2016的谈话,但它并不完全清楚,我的最终值是什么x会(虽然它似乎什么他要说的是,这将是至少1)。

Obviously, i itself will in that case be 2 at the end of the expression. 显然,在这种情况下, i本身将在表达式结尾处为2。

P0145R3 (PDF) does not change the evaluation order of all expressions. P0145R3(PDF)不会更改所有表达式的评估顺序。 It only affects a small number of operators. 它只影响少数运营商。 And binary addition is not on that list. 二进制加法不在该列表中。

Therefore the above code remains undefined. 因此,上述代码仍未定义。

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

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