https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98933
Bug ID: 98933 Summary: P0784R7 example support: constexpr new Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: markus.kuehni at triviso dot ch Target Milestone: --- Trying to recreate the [P0784R7](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r7.html) code example. <source>:13:10: error: 'mark_immutable_if_constexpr' is not a member of 'std' 13 | std::mark_immutable_if_constexpr(this->ps); Removed std::mark_immutable_if_constexpr(this->ps); <source>:22:31: error: no matching function for call to 'S<char>::S(const char [7])' 22 | constexpr S<char> str("Hello!"); | ^ Added std::add_const<T>::type for constructor argument p. <source>:22:31: in 'constexpr' expansion of 'S<char>("Hello!")' <source>:11:7: error: call to non-'constexpr' function 'void* operator new(std::size_t, void*)' 11 | new(this->ps+k) T{p[k]}; Replaced with std::construct_at() and std::destroy_at() for symmetry, see [91369](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91369). In file included from /opt/compiler-explorer/gcc-trunk-20210202/include/c++/11.0.0/memory:64, from <source>:1: /opt/compiler-explorer/gcc-trunk-20210202/include/c++/11.0.0/bits/allocator.h:171:50: error: 'S<char>("Hello!")' is not a constant expression because it refers to a result of 'operator new' 171 | return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); Now I'm stuck. A cleaned-up version is here: https://godbolt.org/z/KWMqna ``` #include <memory> #include <new> template<typename T> struct S : std::allocator<T> { std::size_t sz; T *ps; template<std::size_t N> constexpr S(typename std::add_const<T>::type (&p)[N]) : sz { N }, ps {this->allocate(N)} { for (std::size_t k = 0; k<N; ++k) { std::construct_at(ps+k, p[k]); } } constexpr ~S() { for (std::size_t k = 0; k < this->sz; ++k) { std::destroy_at(ps+k); } this->deallocate(this->ps, this->sz); } }; constexpr S<char> str("Hello!"); // str ends up pointing to a static array // containing the string "Hello!". ```