https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90391
Bug ID: 90391
Summary: nonconforming value initialization when type T has a
base class with a user-defined default constructor
Product: gcc
Version: 9.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: mikedlui+gccbugzilla at gmail dot com
Target Milestone: ---
This is probably a low priority due to its edge-case nature, but here it is:
https://stackoverflow.com/q/54028846/1371191
#include <iostream>
struct A {
A(){}
int i;
};
struct B : public A {
int j;
};
int main() {
B b = {};
std::cout << b.i << b.j << std::endl;
}
Compiling:
$ g++ -std=c++11 -Wuninitialized -O2 a.cpp
a.cpp: In function ‘int main()’:
a.cpp:25:25: warning: ‘b.B::<anonymous>.A::i’ is used uninitialized in this
function [-Wuninitialized]
std::cout << b.i << " " << b.j << std::endl
`b.B::<anonymous>.A::i` should be zero initialized (along with all of `b`) for
C++11, but it looks like it's getting initialized by A's default constructor
instead. This is the case for both C++11 and C++14.
As others mention in the stack overflow responses, this behavior is expected in
only expected in C++17/C++20, but for different reasons.