没有缺少 ctor 初始值设定项列表的警告?

pal*_*ogt 6 c++ valgrind

此代码缺少构造函数初始值设定项列表:

#include <cstdio>

struct s { 
    s() {}  // should be s(): m() {}
    int m;
};

int main() {
    struct s *px = new s();
    if (px->m) {
        printf("true\n");
    } else {
        printf("false\n");
    }
    delete px;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

gcc 编译干净,没有警告:

$ g++ -Wall -Wextra -g -O2 test.cpp
Run Code Online (Sandbox Code Playgroud)

但是,valgrind 知道类成员m尚未初始化:

$ valgrind ./a.out
==10953== Conditional jump or move depends on uninitialised value(s)
==10953==    at 0x400512: main (test.cpp:10)
==10953== 
Run Code Online (Sandbox Code Playgroud)

为什么 gcc 没有警告缺少初始化(-Wmissing-field-initializers-Wuninitialized-Wmaybe-uninitialized)?

我可以通过一个标志来捕获这种情况吗?

Ted*_*gmo 6

您可以添加-Weffc++来捕获它(受到 Scott Meyers 的书“Effective C++”的启发)。奇怪的是,它没有引用任何其他-W选项(也没有clang++)。

然而,有些人认为这个选项现在有点过时了,但在这种情况下,它发现了一个真正的问题。