如何让 GCC/Clang 警告使用未初始化的成员?

How can I make GCC/Clang warn about the use of uninitialized members?

我正在编译后面的代码

class Test {
public:
    Test() {}

    int k;
};

int main() {
  Test t;
  std::cout << t.k << "\n";
}

喜欢

g/clang++ main.cpp -Wall -Wextra --std=c++14 -o exe; ./exe

为什么两个编译器都不警告我不确定的整数值,这不是一个非常严重的潜在错误吗?如何为不确定的初始化启用警告?

对于这个例子,当我给它 -O1(或更高)时,GCC gives me the desired warning

大概它用于检测此问题的任何机制都以某种方式与优化工作级别相关联。 It's a notoriously hard thing to do.

确保您注意 release-build 警告以及 debug-build 警告。