我编译了这个简单的程序:
#include <cstdio>
#include <iostream>
using namespace std;
struct Foo
{
int a;
int b;
};
struct Bar
{
//Bar() = default;
int d;
};
int main()
{
Foo foo;
Bar bar;
printf("%d %d\n", foo.a, foo.b);
return 0;
}
我收到了这些警告:
$ g++ -std=c++11 -Wall -Wextra -Wpedantic foo.cpp -o foo
foo.cpp: In function ‘int main()’:
foo.cpp:21:9: warning: unused variable ‘bar’ [-Wunused-variable]
Bar bar;
^
foo.cpp:23:11: warning: ‘foo.Foo::b’ is used uninitialized in this function [-Wuninitialized]
printf("%d %d\n", foo.a, foo.b);
^
foo.cpp:23:11: warning: ‘foo.Foo::a’ is used uninitialized in this function [-Wuninitialized]
当然,这是我们所期望的。但是当我取消注释 Bar
默认 ctor 时,出现了问题 - 所有警告都消失了。
为什么 Bar
ctor 会禁用 Foo
的警告?
我的 GCC 版本是:g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
.
在 C++03 上不会出现此问题,仅在 C++11 或更高版本上出现。
最佳答案
这是一个编译器错误,正如 Jarod 所指出的,已修复。
关于c++ - 未初始化成员的警告在 C++11 上消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40258008/