gpt4 book ai didi

c++ - g++ -Wreorder 的意义何在?

转载 作者:IT老高 更新时间:2023-10-28 11:32:28 25 4
gpt4 key购买 nike

g++ -Wall 选项包括 -Wreorder。该选项的作用如下所述。我不清楚为什么有人会关心(尤其是在 -Wall 中默认打开它)。

-Wreorder (C++ only)  Warn when the order of member initializers given in the code does not  match the order in which they must be executed.  For instance:    struct A {      int i;      int j;      A(): j (0), i (1) { }    };  The compiler will rearrange the member initializers for i and j to  match the declaration order of the members, emit-ting a warning to that  effect.  This warning is enabled by -Wall.

最佳答案

考虑:

struct A {
int i;
int j;
A() : j(0), i(j) { }
};

现在 i 被初始化为某个未知值,而不是零。

另外,i 的初始化可能会产生一些副作用,这对顺序很重要。例如

A(int n) : j(n++), i(n++) { }

关于c++ - g++ -Wreorder 的意义何在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1828037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com