According to: constexpr a static data member giving an undefined reference error static members of the constexpr class must satisfy two requirements:
template <typename Tp> struct wrapper { static constexpr Tp value{};
- Initialized inside the class definition (since it is constexpr)
- Defined outside the class definition (because it is static)
If I changed 1. to uniform initialization
template <typename Tp> struct wrapper { static constexpr auto value = Tp{};
the compiler complains about conflicting declarations:
$ g++ prog.cc -Wall -Wextra -std=c++1z -pedantic prog.cc:7:31: error: conflicting declaration 'constexpr const Tp wrapper<Tp>::value' constexpr Tp wrapper<Tp>::value; prog.cc:3:29: note: previous declaration as 'constexpr const auto wrapper<Tp>::value' static constexpr auto value = Tp{};
and also skip the initializer:
prog.cc:7:31: error: declaration of 'constexpr const auto wrapper<Tp>::value' has no initializer
Removing conflicting 2. Definition ends, as expected, with a linker error:
In function `main': prog.cc:(.text+0x8): undefined reference to `wrapper<foo>::value'
Sample code online .
Is it possible / legally to use a single initialization for static members of the constexpr class?
c ++ c ++ 11 static-members uniform-initialization constexpr
Karol Wozniak
source share