Uniform initialization of a static member constexpr

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{}; // 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 (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{}; // uniform initialization }; template<typename Tp> constexpr Tp wrapper<Tp>::value; 

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?

+7
c ++ c ++ 11 static-members uniform-initialization constexpr
source share
1 answer

It may be my misunderstanding, but I would appreciate

 struct wrapper { static constexpr Tp value = Tp{}; }; 

- an example of uniform initialization. Indeed, the first code example is also uniform initialization. The standard itself simply requires that these static constexpr members be initialized with a bracket or assignment expression. This, as you have already seen, works great.

The problem seems to be related to type inference from auto in the context of the template, and I suspect this is an implementation error, although the standard is large and I might be missing something.

If the size of the right-hand side of the constexpr initialization was an expression with a hard preliminary type definition, a workaround would be to use decltype , for example.

 template <typename Tp> struct wrapper { static constexpr decltype(complex-init-expr) value = complex-init-expr; }; template <typename Tp> static constexpr decltype(complex-init-expr) wrapper<Tp>::value; 

or

 template <typename Tp> struct wrapper { typedef decltype(complex-init-expr) value_type; static constexpr value_type value = complex-init-expr; }; template <typename Tp> static constexpr typename wrapper<Tp>::value_type wrapper<Tp>::value; 
0
source share

All Articles