为什么关于未初始化变量的g ++警告取决于变量的类型?(它警告一个int但不是一个双)

Som*_*ody 12 c++ g++

我目前正在尝试理解g ++在哪些情况下警告未初始化的变量.考虑以下代码:

#include <iostream>

typedef double barType;

struct foo {

    barType bar;

};

int main() {

    foo f;

    std::cout << f.bar << std::endl;

}
Run Code Online (Sandbox Code Playgroud)

如果我像这样编译它我没有得到警告:

$ g++ -O1 -Wall test.cc -o test
Run Code Online (Sandbox Code Playgroud)

但如果我将barType更改为int:

$ g++ -O1 -Wall test.cc -o test
  test.cc: In function ‘int main()’:
  test.cc:17: warning: ‘f.foo::bar’ is used uninitialized in this function
Run Code Online (Sandbox Code Playgroud)

警告如何取决于类型?在这两种情况下都没有初始化.

我正在使用:

$ g++ --version
g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

谢谢,

Gen*_*yev 11

它是未定义的行为,不需要诊断,因此编译器可以自由地做出判断.他们本可以做得更好.


Dan*_*Dan 1

据猜测,他们可能更关心未初始化的整数类型,而不是浮点数或双精度数,因为您可以使用带有指针偏移量的整数类型而不进行强制转换,这可能非常糟糕(tm)