简体   繁体   English

为什么C ++ 17 GCC编译器会发出关于undefined的警告?

[英]Why does C++17 GCC compiler gives warning about undefined?

According to C++17, there is no guarantee for order of evaluation in following expression. 根据C ++ 17,不能保证下面的表达式中的评估顺序。 It is called unspecified behaviour. 它被称为未指定的行为。

int i = 0;
std::cout<<i<<i++<<std::endl;

C++17 GCC compiler gives following warning: Live Demo C ++ 17 GCC编译器提供以下警告: Live Demo

prog.cc: In function 'int main()':
prog.cc:6:20: warning: operation on 'i' may be undefined [-Wsequence-point]
     std::cout<<i<<i++<<std::endl;

I don't understand, in c++17 above express no longer undefined behaviour, then Why does compiler gives warning about undefined? 我不明白,在上面的c ++ 17中表达不再是未定义的行为,那么为什么编译器会发出关于undefined的警告?

Seems like gcc gives a warning because this is a corner case, or at least very close to being one. 似乎像gcc一样发出警告,因为这是一个极端情况,或者至少非常接近于一个。 Portability seems to be one concern. 便携性似乎是一个问题。

From the page https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html 从页面https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

The C++17 standard will define the order of evaluation of operands in more cases: in particular it requires that the right-hand side of an assignment be evaluated before the left-hand side, so the above examples are no longer undefined. 在更多情况下,C ++ 17标准将定义操作数的评估顺序:特别是它要求在左侧之前评估赋值的右侧,因此上面的示例不再是未定义的。 But this warning will still warn about them, to help people avoid writing code that is undefined in C and earlier revisions of C++. 但是这个警告仍然会警告他们,以帮助人们避免编写C语言和早期版本的C ++中未定义的代码。

The standard is worded confusingly, therefore there is some debate over the precise meaning of the sequence point rules in subtle cases. 该标准令人困惑,因此在细微的情况下对序列点规则的确切含义存在争议。 Links to discussions of the problem, including proposed formal definitions, may be found on the GCC readings page, at http://gcc.gnu.org/readings.html . 有关问题讨论的链接,包括拟议的正式定义,可在GCC阅读页面http://gcc.gnu.org/readings.html上找到

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

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