Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uniform initialization of static constexpr member

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;    
}
  1. Initialized inside the class definition (because it is constexpr)
  2. 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?

like image 474
Karol Wozniak Avatar asked Mar 04 '15 20:03

Karol Wozniak


1 Answers

This may be my misunderstanding, but I would have regarded

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

to be an example of uniform initialisation. Indeed, the first code example is also uniform initialisation. The standard itself just demands that these static constexpr members be initialised with a brace-or-assignment expression. This, as you've already seen, works fine.

The problem seems to be with the type deduction from auto in a template context, and I suspect it is an implementation bug, though the standard is big, and I could easily have missed something.

If the right hand size of the constexpr initialisation were an expression with a hard to pre-determine type, a workaround would be to use decltype, e.g.

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;
like image 80
halfflat Avatar answered Nov 05 '22 19:11

halfflat