According to: constexpr static data member giving undefined reference error static constexpr class members must meet two requirements:
template <typename Tp>
struct wrapper {
static constexpr Tp value{}; // 1
};
template<typename Tp>
constexpr Tp wrapper<Tp>::value; // 2
struct foo {
};
int main() {
auto const& x = wrapper<foo>::value;
(void)x;
}
- Initialized inside the class definition (because it is constexpr)
- Defined outside the class definition (because it is static)
If I change 1. to uniform initialization
template <typename Tp>
struct wrapper {
static constexpr auto value = Tp{}; // uniform initialization
};
template<typename Tp>
constexpr Tp wrapper<Tp>::value;
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 about missing 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 linker error:
In function `main': prog.cc:(.text+0x8): undefined reference to `wrapper<foo>::value'
Code example online.
Is it possible/legal to use uniform initialization for static constexpr class members?