https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122899

            Bug ID: 122899
           Summary: constexpr error with self-referencing struct
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

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

Reply via email to