Bug 80681 - missing -Wuninitialized for const or reference member of a private base class
Summary: missing -Wuninitialized for const or reference member of a private base class
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, easyhack
Depends on:
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2017-05-08 22:31 UTC by Martin Sebor
Modified: 2021-04-01 23:31 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 10.2.0, 11.0, 7.3.0, 8.3.0, 9.2.0
Last reconfirmed: 2021-04-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2017-05-08 22:31:48 UTC
G++ issues -Wuninitialized for uninitialized private const data or reference members of classes with no constructors because there is no other way to initialize them.

However, G++ neglects to issue the same warning when an uninitialized public const data or reference member is defined in a base class that is privately derived by a class without constructors, even though such a member also cannot be initialized.

In addition, the C++ warning is not entirely correctly documented.  The manual states that:

  In C++, warn if a non-static reference or non-static const member appears in a class without constructors. 

However, G++ only issues the warning when the member is inaccessible (private or protected).


$ cat y.C && gcc -S -Wall -Wextra y.C
struct A1 { private: const int i; };    // warning, good

struct B1 { const int j; };             // no warning, good

struct C1: private B1 { };              // bug: missing warning


struct A2 { private: const int &i; };   // warning, good

struct B2 { const int &j; };            // no warning, good

struct C2: private B2 { };              // bug: missing warning

y.C:1:32: warning: non-static const member ‘const int A1::i’ in class without a constructor [-Wuninitialized]
 struct A1 { private: const int i; };    // warning, good
                                ^
y.C:8:33: warning: non-static reference ‘const int& A2::i’ in class without a constructor [-Wuninitialized]
 struct A2 { private: const int &i; };   // warning, good
                                 ^
Comment 1 Eric Gallager 2017-08-23 12:51:34 UTC
Confirmed.
Comment 2 Eric Gallager 2018-09-22 03:14:08 UTC
cc-ing Manu since he is pretty knowledgeable about -Wuninitialized
Comment 3 Manuel López-Ibáñez 2018-09-22 10:26:03 UTC
This should be easy to do since at the point of warning (which happens in the FE), all information needed is available.
Comment 4 Martin Sebor 2021-04-01 23:31:34 UTC
No progress in GCC 11.