Код: Выделить всё
template 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 constexpr Foo foo(T t) {
return Foo(t);
}
constexpr auto f = foo(1);
static_assert(f.get_t() == 1);
Код: Выделить всё
:16:25: error: non-constant condition for static assertion
16 | static_assert(f.get_t() == 1);
| ~~~~~~~~~~^~~~
:16:22: in 'constexpr' expansion of 'f.Foo::get_t()'
:8:17: error: the value of 'f' is not usable in a constant expression
8 | return *t_ptr;
| ^~~~~
:15:16: note: 'f' used in its own initializer
15 | constexpr auto f = foo(1);
| ^
Compiler returned: 1
Подробнее здесь: https://stackoverflow.com/questions/798 ... ing-struct
Мобильная версия