2
votes

C++ can initialize a constant static members in the class declaration only if the member is a non-volatile const integral type.

But why integral type? Is there any implementation issues for other types like floating numbers? precision maybe?

struct testClass
{
    static const int val = 12; 
    //static const float val = 12; 
};
1

1 Answers

3
votes

Caution. It's not always easy (or even feasible) for the compiler to emulate the floating-point implementation on the target, and anyway floating-point behaviour on the target might be dynamically changed by changing the rounding mode.

So anything involving compile-time floating point values is tricky. C++03 does nothing that might encourage it ;-)

C++11 allows what you want with constexpr in place of const.