我编译这个简单的程序:
#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,有一个问题-所有警告都消失了。
Why the Bar
ctor 禁用警告Foo
?
我的海湾合作委员会版本是:g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
.
该问题不会出现在 C++03 上,仅出现在 C++11 或更高版本上。
这是一个编译器错误,正如 Jarod 指出的那样,该错误已被修复。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)