简体   繁体   English

在 C++17 中,此代码是否应该产生警告?

[英]In C++17 should this code be producing warnings?

As seen on this godbolt link正如在这个godbolt链接上看到的

clang in c++14 mode(but not in c++17) and GCC in c++17 mode produce warnings about sequencing. clang 在 c++14 模式(但不是在 c++17 中)和 GCC 在 ZF4A2D1DA1DA520DF2ZA 模式下产生警告约 7 个 E 排序。 I assumed that in C++17 all stuff on the rhs of = is evaluated before lhs so I am not sure if the gcc warning is correct.我假设在 C++17 中,= 的 rhs 上的所有东西都在 lhs 之前评估,所以我不确定 gcc 警告是否正确。

Code is:
static int index =0; 
void f(int* pindex){
        pindex[index] = 5;
        pindex[index] = index++;

}
int main(){

}

gcc warning is: gcc 警告是:

: In function 'void f(int*)': :在 function 'void f(int*)' 中:
:4:30: warning: operation on 'index' may be undefined [-Wsequence-point] :4:30: 警告:对“索引”的操作可能未定义 [-Wsequence-point]
 4 | pindex[index] = index++; | ~~~~~^~
:4:30: warning: operation on 'index' may be undefined [-Wsequence-point] :4:30: 警告:对“索引”的操作可能未定义 [-Wsequence-point]

Compiler returned: 0编译器返回:0

note: I know that standard specifies nothing about warnings, it is just much easier to specify the question wrt warnings, than to talk about sequence point/ordering guarantees.注意:我知道标准没有指定任何关于警告的内容,只是指定问题 wrt 警告比谈论序列点/排序保证要容易得多。

This code could produce warnings before C++17 where it was undefined behavior, but should not with C++17 or later as the behavior became defined:此代码可能会在 C++17 之前产生警告,它是未定义的行为,但不应该与 C++17 或更高版本的行为定义:

  1. In every simple assignment expression E1=E2 and every compound assignment expression E1@=E2 , every value computation and side-effect of E2 is sequenced before every value computation and side effect of E1 (since C++17)在每个简单赋值表达式E1=E2和每个复合赋值表达式E1@=E2 E2 ,E2 的每个值计算和副作用都排在E1的每个值计算和副作用之前(C++17 起)

( Source ) 来源

GCC's warning is a bug. GCC 的警告是一个错误。 (Hopefully this is the only bug and GCC doesn't actually treat this case as UB.) (希望这是唯一的错误,GCC 实际上并没有将这种情况视为 UB。)

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

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