Why must non-integral static data members initialized in the class be constexpr?

。_饼干妹妹 提交于 2019-12-04 22:33:03

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).

float is a bit of a harder one to describe the motivation for, but imagine a class member:

class MySpecialInt {
public:
    constexpr MySpecialInt(const int & other) {
    }
};
class MyClass {
    static const     MySpecialInt a = 5; // error
    static constexpr MySpecialInt b = 5; // okay
};

a in this scenario could have some non-trivial construction that potentially violates (or at least grossly complicates) the one-definition-rule. Because constexpr has guaranteed restrictive compile-time properties, b's copy constructor must also be constexpr and is therefore guaranteed to return a well defined value at compile-time (and NOT violate the one-definition-rule)

Why float exhibits this behavior I believe is just for legacy reasons since float has never been traditionally initialize-able like this ("because the standard says so"), so they caught initializing static const float members under the umbrella of constexpr.

It may be because of the fact that non integral i may also includes data type like char and that's why you can't make them constant and requires constant expression.But in case of integral, you can either make them constant expression or constant. So, because of fact that char can only be a constant expression, so it is illegal for all non integral values.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!