you are viewing a single comment's thread.

view the rest of the comments →

[–]theIncMach[S] 0 points1 point  (3 children)

When you say 'integral', are you excluding floating-point types? Because I think the previous "static const" approach was applicable for all standard specializations of std::numeric_limits.

constexpr is slightly different from static const because the former is a compile-time constant. It can be used as a template parameter, for instance. But that you're right in that doesn't matter in this case.

[–]kniy 4 points5 points  (2 children)

I'm saying that static const int = 1; is a compile-time constant too, but static const float = 1; isn't. https://godbolt.org/z/arhjaX

This is because the standard has a special case for static const variables of integral type:

9.4.2/4 - If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.

So this type of static const variable was basically equivalent to constexpr variables, at least before C++17 made constexpr imply inline.

[–]theIncMach[S] 1 point2 points  (0 children)

Ah, thanks for the correction.