[英]Warnings for uninitialized members disappear on the C++11
我编译这个简单的程序:
#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指出的那样,已被修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.