[英]Warnings for uninitialized members disappear on the C++11
I compile this simple program: 我编译这个简单的程序:
#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;
}
and I get those warnings: 我得到了那些警告:
$ 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]
Of course, this is what we expect. 当然,这是我们所期望的。 But when I uncomment the
Bar
default ctor, there is a problem - all warnings disappear. 但是当我取消对
Bar
默认ctor的注释时,会出现问题 - 所有警告都会消失。
Why the Bar
ctor disables warnings for Foo
? 为什么
Bar
ctor会禁用Foo
警告?
My GCC version is: g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
. 我的GCC版本是:
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
。
The problem does not occur on the C++03, only on the C++11 or newer. 问题不会发生在C ++ 03上,只发生在C ++ 11或更新版本上。
这是一个编译器错误,正如Jarod指出的那样,已被修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.