我目前正试图了解在哪些情况下g++会警告未初始化的变量。考虑以下代码片段:
#include <iostream>
typedef double barType;
struct foo {
barType bar;
};
int main() {
foo f;
std::cout << f.bar << std::endl;
}
如果我以这种方式编译,就不会收到任何警告:
$ g++ -O1 -Wall test.cc -o test
但如果我将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
这个警告如何与类型有关?在两种情况下都未初始化。
我正在使用:
$ 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.
谢谢,
某人