mch*_*mch 13 c++ sequence-points language-lawyer c++17
我读了C++ 17 Standard $ 8.5.7.4:
表达E1在表达E2之前测序.
换班运营商.
此外cppreference第19条说:
In a shift operator expression E1<<E2 and E1>>E2, every value
computation and side-effect of E1 is sequenced before every value
computation and side effect of E2
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用gcc 7.3.0或clang 6.0.0编译以下代码时
#include <iostream>
using namespace std;
int main() {
int i = 5;
cout << (i++ << i) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下gcc警告:
../src/Cpp_shift.cpp: In function ‘int main()’:
../src/Cpp_shift.cpp:6:12: warning: operation on ‘i’ may be undefined [-Wsequence-point]
cout << (i++ << i) << endl;
~^~
Run Code Online (Sandbox Code Playgroud)
铿锵警告是:
warning: unsequenced modification and access to 'i' [-Wunsequenced]
Run Code Online (Sandbox Code Playgroud)
我使用以下命令编译:
g++ -std=c++17 ../src/Cpp_shift.cpp -o Cpp_shift -Wall
clang++ -std=c++17 ../src/Cpp_shift.cpp -o Cpp_shift -Wall
Run Code Online (Sandbox Code Playgroud)
320在两种情况下我得到预期的输出(5*2 ^ 6)
有人可以解释为什么我会收到此警告吗?我忽略了什么吗?我也读过这个相关的问题,但它没有回答我的问题.
编辑:所有其他变体++i << i,i << ++i并i << i++导致相同的警告.
edit2:(i << ++i)结果320为clang(正确)和384gcc(不正确).似乎gcc给出了错误的结果,如果++它处于E2,(i << i++)也会给出错误的结果.
| 归档时间: |
|
| 查看次数: |
397 次 |
| 最近记录: |