Bug 97128 - Uninitialized members of base class wrongly allowed in constexpr constructor in c++17 mode
Summary: Uninitialized members of base class wrongly allowed in constexpr constructor ...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 11.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL: https://godbolt.org/z/dWP1sh
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2020-09-21 01:10 UTC by Fedor Alekseev
Modified: 2021-04-15 15:14 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-04-15 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fedor Alekseev 2020-09-21 01:10:37 UTC
Given this code: (https://godbolt.org/z/qsaeM7)

struct Base {
    int x;
};

struct Derived : Base {
    constexpr Derived() { }
};

int main () {
    constexpr Derived kDerived;
    return kDerived.x;
}

g++ -std=c++17 -pedantic compiles the program that exits with code 0.
clang rejects it as I believe in c++17 for a constructor to be constexpr all members must be initialized before execution enters the body of the constructor.

I also believe that the program is invalid as of c++20 as all must be initialized by the end of the constexpr constructor. Still g++ -std=c++2a compiles it.

Note that if there was an uninitialized member of Derived itself, g++ would generate an error as expected.

There is a similar bug filed where g++ wrongly allows uninitialized member of anonymous struct inside a union member: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86581 . Not sure if this counts as a duplicate.
Comment 1 Fedor Alekseev 2020-09-21 01:12:55 UTC
Sorry, the link in the initial comment led to a slightly less minimal example. Here's a link to the code from the initial comment in CE: https://godbolt.org/z/dWP1sh
Comment 2 Fedor Alekseev 2020-09-21 01:19:14 UTC
Also my initial claim about c++20 was wrong, gcc 10+ generates the error when provided with -std=c++2a switch.
Still there is a problem in c++17 mode.
Comment 3 Martin Sebor 2021-04-15 00:30:44 UTC
Confirmed.  If it's a regression I can't tell when it happened.  See also pr99700 for a similar test case in C++ 20 mode.