My two cents, while looking at some documentation like that one.
Actually, it could be valid.
In fact, the difference between constexpr
and const
mainly relies into their purposes, but the former implies the latter as a side effect.
There is also a more subtle difference: constexpr
is a specifier, while const
is a type qualifier.
In particular:
const’s primary function is to express the idea that an object is not modified through an interface
On the other side:
constexpr’s primary function is to extend the range of what can be computed at compile time, making such computation type safe and also usable in compile-time contexts
Or even more concise from here:
constexpr - specifies that the value of a variable or function can appear in constant expressions
Anyway it happens that:
constexpr in front of a variable definition [...] implies const
So, even though the reason for which one should use constexpr
instead of const
is clear, and everybody tends to remember that:
constexpr is not a general purpose replacement for const (or vice versa)
It's a fact that using constexpr
you are actually saying that this member is implicitly const
(also you have a set of more specific constraints on how that member can be defined).
Anyway, once defined, the member is nothing more than a data member having the const
type qualifier (which can be used in constant expressions), that is what you are declaring out of your class.