Warnings for uninitialized members disappear on the C++11

假装没事ソ 提交于 2019-12-10 12:33:55

问题


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.


回答1:


It's a compiler bug, which as Jarod pointed out, has been fixed.



来源:https://stackoverflow.com/questions/40258008/warnings-for-uninitialized-members-disappear-on-the-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!