Proper initialization of static constexpr array in class template?

monkey0506 picture monkey0506 · Jan 18, 2013 · Viewed 10.8k times · Source

Static class members in C++ have caused a little confusion for me due to the standard's verbiage:

9.4.2 Static data members [class.static.data]

The declaration of a static data member in its class definition is not a definition...

However a constexpr is required to be initialized (AFAIK, couldn't find a quote from the standard) at its declaration (e.g., in the class definition).

Because of the restrictions on constexpr I had actually forgotten about the requisite for static members to be defined outside of the class, until I tried accessing a static constexpr array. This related question provides the correct way of defining the array member, but I'm interested as to the implications on this definition in a class template.

This is what I ended up with:

template<typename T>
class MyClass
{
private:
  static constexpr std::size_t _lut[256] = { /* ... */ };
  T _data;

public:
  static constexpr std::size_t GetValue(std::size_t n) noexcept
  {
    return _lut[n & 255];
  }

  // ...
};

template<typename T>
constexpr std::size_t MyClass<T>::_lut[256];

Is this the right syntax? Particularly the use of template in the definition feels awkward, but GCC seems to be linking everything appropriately.

As a follow-up question, should non-array static constexpr members be similarly defined (with template definition outside of class)?

Answer

arr_sea picture arr_sea · Mar 14, 2014

In case it helps anyone out, the following worked for me with GCC 4.7 using constexpr:

template<class T> class X {
  constexpr static int s = 0;
};
template<class T> constexpr int X<T>::s; // link error if this line is omitted

I'm not making any claims of whether this is "proper". I'll leave that to those more qualified.