C++ – Why must non-integral static data members initialized in the class be constexpr

cc++11constexprstatic-members

Static integral data members initialized in the class definition may be declared const or constexpr, but non-integral static data members initialized in the class definition must be constexpr:

class MyClass {
  static const     int   w = 5;          // okay
  static constexpr int   x = 5;          // okay
  static const     float y = 1.5;        // error!
  static constexpr float z = 1.5;        // okay
};

Does anybody know why the declaration for y is not permitted? The part of the Standard making it illegal is 9.4.2/3, but why is it illegal?

Best Answer

Prior to C++11, you could not initialize static members of non-integral/enumeration types in the class declaration (but you can outside of the class declaration). The rule governing constexpr carries that forward, but allows you to initialize it using constexpr in the class declaration (so you don't need code like the following anymore):

struct A
{
    static const float pi;
};

const float A::pi = 3.1415;

One of the side effects of this rule was to simplify your class structure instead of making it ugly (like the above code).

One of the reasons why this was the case prior to C++11's addition of constexpr was the standard did not specify how floating points were to be implemented (it is left to the processor/architecture - for example, when you say float x = 1.6f, it is actually 1.6000000000024 on most systems).