https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108897
Bug ID: 108897 Summary: Comparing pointer to field in rvalue class is not considered constant expression Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: danakj at orodu dot net Target Milestone: --- This reproduces on godbolt in GCC 10, 11, 12, and trunk. A pointer to a field of a structure that is constructed as an rvalue temporary object can not be compared in a constant expression. ``` struct S { int i; int *p = &i; constexpr const int* ptr() const { return &i; } }; // Not constant expression in GCC. static_assert(S{2}.ptr() != nullptr); static_assert(S{2}.p != nullptr); // Is a constant expression. static_assert(*S{2}.ptr() == 2); static_assert(*S{2}.p == 2); static_assert([]() constexpr { S r(2); // Is a constant expression. return r.ptr() != nullptr && r.p != nullptr; }()); ``` Errors: <source>:8:26: error: non-constant condition for static assertion 8 | static_assert(S{2}.ptr() != nullptr); | ~~~~~~~~~~~^~~~~~~~~~ <source>:8:26: error: '((&<anonymous>.S::i) != 0)' is not a constant expression <source>:9:22: error: non-constant condition for static assertion 9 | static_assert(S{2}.p != nullptr); | ~~~~~~~^~~~~~~~~~ <source>:9:22: error: '((&<anonymous>.S::i) != 0)' is not a constant expression - Generates errors for comparing `ptr()` or `p` to nullptr. - Dereferencing the pointer does not generate an error. - If the object S is stored as an lvalue, then the pointer comparison is a constant expression. Godbolt: https://godbolt.org/z/P9GGKeb3Y