https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125906
Bug ID: 125906
Summary: -Wuninitialized false positive when accessing a
virtual base class member in a member initializer of
an abstract class constructor
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: Steffen.Kiess at ipvs dot uni-stuttgart.de
Target Milestone: ---
In all gcc versions starting with gcc 14 the following code will warn when
compiled with "-Wall -O":
struct C1 {
int m1;
C1() : m1(42) {}
};
struct C2 : virtual C1 {
int m2;
C2();
virtual void makeAbstract() = 0;
};
C2::C2() : m2(this->m1) {}
struct C3 : C2 {
C3() {}
void makeAbstract() {}
};
int main() { C3 c; }
The output for gcc 14.2.0 (with -Wall -O) is:
file.cpp: In constructor ‘C2::C2()’:
file.cpp:10:21: warning: ‘((C1*)this)[3].C1::m1’ is used uninitialized
[-Wuninitialized]
10 | C2::C2() : m2(this->m1) {}
| ~~~~~~^~
As far as I understand the C++ standard the C1 constructor should always be
executed before non-static member initializers of C2, meaning this warning is
incorrect.
Note that making C2 non-abstract removes the warning for some reason. Making
C2::C2 inline also removes the warning.
I also reported a a similar (but slightly different) bug report for clang at
<https://github.com/llvm/llvm-project/issues/204858>.