https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126358
Bug ID: 126358
Summary: Modifying derived member in base constructor in
constant expression not rejected
Product: gcc
Version: 16.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: gongke at ios dot ac.cn
Target Milestone: ---
Consider this code:
```cpp
#include <cassert>
struct Base {
constexpr Base(int* p) {
*p = 42; // UB: modifies storage before Derived::x's lifetime begins
}
};
struct Derived : Base {
int x;
constexpr Derived() : Base(&x) {} // UB: forms pointer to x before
construction starts
};
int main() {
constexpr Derived d; // ill-formed: constant evaluation encounters UB
return d.x;
}
```
This modification `*p = 42` is undefined behavior and should be rejected in
constant evaluation. Clang correctly rejects this while GCC doesn't.
[godbolt](https://godbolt.org/z/qYevG1bP6)