为什么 C++17 GCC 编译器给出未定义的警告?
Why does C++17 GCC compiler gives warning about undefined?
根据 C++17,无法保证以下表达式中的求值顺序。它被称为未指定的行为。
int i = 0;
std::cout<<i<<i++<<std::endl;
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;
我不明白,在上面的c++17中express不再是未定义的行为,那么为什么编译器会给出未定义的警告?
gcc 似乎发出了警告,因为这是一个极端情况,或者至少非常接近于一个极端情况。便携性似乎是一个问题。
来自页面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. But this warning will still warn about them, to help people avoid writing code that is undefined in C and earlier revisions of 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.
根据 C++17,无法保证以下表达式中的求值顺序。它被称为未指定的行为。
int i = 0;
std::cout<<i<<i++<<std::endl;
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;
我不明白,在上面的c++17中express不再是未定义的行为,那么为什么编译器会给出未定义的警告?
gcc 似乎发出了警告,因为这是一个极端情况,或者至少非常接近于一个极端情况。便携性似乎是一个问题。
来自页面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. But this warning will still warn about them, to help people avoid writing code that is undefined in C and earlier revisions of 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.