Bug 122899 - constexpr error with self-referencing struct
Summary: constexpr error with self-referencing struct
Status: RESOLVED DUPLICATE of bug 64989
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2025-11-28 09:04 UTC by Fedor Chelnokov
Modified: 2025-11-28 20:11 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fedor Chelnokov 2025-11-28 09:04:37 UTC
This program
```
template <class T> class Foo {
    T t;
    const T* t_ptr;
public:
    constexpr Foo(T t): t(t), t_ptr(&this->t) {}
    constexpr Foo(const Foo& foo): t(foo.t), t_ptr(&this->t) {}
    constexpr T get_t() const {
        return *t_ptr;
    }
};
template <class T> constexpr Foo<T> foo(T t) {
    return Foo<T>(t);
}

constexpr auto f = foo(1);
static_assert(f.get_t() == 1);
```
is accepted by Clang, EDG and MSVC. But GCC complains:
```
<source>:16:25: error: non-constant condition for static assertion
   16 | static_assert(f.get_t() == 1);
      |               ~~~~~~~~~~^~~~
<source>:16:22:   in 'constexpr' expansion of 'f.Foo<int>::get_t()'
<source>:8:17: error: the value of 'f' is not usable in a constant expression
    8 |         return *t_ptr;
      |                 ^~~~~
<source>:15:16: note: 'f' used in its own initializer
   15 | constexpr auto f = foo(1);
      |  
```
Online demo: https://gcc.godbolt.org/z/hha5ceqb1

Original report: https://stackoverflow.com/q/79831573/7325599
Comment 1 Drea Pinski 2025-11-28 20:11:06 UTC
Dup.

*** This bug has been marked as a duplicate of bug 64989 ***