未初始化成员的警告在C ++ 11上消失 [英] Warnings for uninitialized members disappear on the C++11
本文介绍了未初始化成员的警告在C ++ 11上消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编译这个简单的程式:
#include #include< iostream>
使用namespace std;
struct Foo
{
int a;
int b;
};
struct Bar
{
// Bar()= default;
int d;
};
int main()
{
Foo foo;
酒吧;
printf(%d%d \ n,foo.a,foo.b);
返回0;
}
我得到这些警告:
$ g ++ -std = c ++ 11 -Wall -Wextra -Wpedantic foo.cpp -o foo
foo.cpp:在函数'int main() ':
foo.cpp:21:9:警告:未使用的变量'bar'[-Wunused-variable]
酒吧栏;
^
foo.cpp:23:11:warning:'foo.Foo::b'在此函数中未初始化使用[-Wuninitialized]
printf(%d%d \ n,foo.a,foo.b);
^
foo.cpp:23:11:warning:'foo.Foo::'在此函数中未初始化使用[-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指出的那样,已经修复。
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.
Why the Bar
ctor disables warnings for Foo
?
My GCC version is: 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.
解决方案
It's a compiler bug, which as Jarod pointed out, has been fixed.
这篇关于未初始化成员的警告在C ++ 11上消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文