c++ - 未初始化成员的警告在 C++11 上消失

标签 c++ c++11 gcc static-analysis

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

#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 所指出的,已修复。

关于c++ - 未初始化成员的警告在 C++11 上消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40258008/

相关文章:

c++ - 用于检查给定类型是否存在 istream 运算符 >> 的类型特征

c++ - 使用 gcc 编译 DLL

C++:_popen 无限循环 (Windows)

使用 O1 优化时的 C++ 链接器错误

c++ - QRect 的 qHash 函数

c++ - 在执行 SFINAE 时访问模板化派生类 (CRTP) 的静态函数时类型不完整

c++ - 为什么 std::forward 返回 static_cast<T&&> 而不是 static_cast<T>?

c++ - 为什么在以下情况下没有数据竞争?

linux - 为什么 GNU 链接器找不到带有 -l<library> 的共享对象?

c++ - 构建 GLib 时出错