[英]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.