对于未初始化成员的警告在c++ 11中消失

Warnings for uninitialized members disappear on the C++11

本文关键字:c++ 消失 警告 于未 初始化 成员      更新时间:2023-10-16

我编译了这个简单的程序:

#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 %dn", 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 %dn", foo.a, foo.b);
           ^
foo.cpp:23:11: warning: ‘foo.Foo::a’ is used uninitialized in this function [-Wuninitialized]

当然,这是我们所期望的。但是,当我取消注释Bar默认变量时,有一个问题-所有警告都消失了。

为什么Bar会禁用Foo的警告?

我的GCC版本是:g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609 .

这个问题不会在c++ 03上出现,只会在c++ 11或更新的版本上出现

这是一个编译器错误,正如Jarod指出的,已经修复了。