[Bug c++/124275] New: GCC rejects trivially copying a non-constexpr class object containing nullptr_t in constant initialization
de34 at live dot cn
gcc-bugzilla@gcc.gnu.org
Fri Feb 27 02:25:30 GMT 2026
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124275
Bug ID: 124275
Summary: GCC rejects trivially copying a non-constexpr class
object containing nullptr_t in constant initialization
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Keywords: rejects-valid
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: de34 at live dot cn
Target Milestone: ---
GCC rejects this following code snippet (https://godbolt.org/z/qYdxjPjWo):
```
struct X {
constexpr X() = default;
constexpr X(const X&) = default;
decltype(nullptr) n;
};
extern X x;
constexpr X y = x;
```
But it accepts this one with the copy constructor is made non-trivial (while
still doing the same thing) (https://godbolt.org/z/z3edEWrYM):
```
struct X {
constexpr X() = default;
constexpr X(const X& other) noexcept : n(other.n) {}
decltype(nullptr) n;
};
extern X x;
constexpr X y = x;
```
Per https://eel.is/c++draft/expr.const ,
> - an lvalue-to-rvalue conversion unless it is applied to
> - a glvalue of type cv std::nullptr_t,
it should be valid to copy a non-constant `nullptr_t` object in constant
evaluation. Given the initialization of `y` selects a `constexpr` constructor,
it should be valid.
This might be related to PR 118661 but is not the same. It's strange that MSVC
and Clang behave similarly. Corresponding bugs are already reported.
More information about the Gcc-bugs
mailing list