3 ms·
The const and constexpr keywords mean different things. Declaring a method const tells the compiler that that method won’t change the object. Declaring someth
by gumby 5y ago
The const and constexpr keywords mean different things.
Declaring a method const tells the compiler that that method won’t change the object.
Declaring something constexpr tells the compiler that if its arguments are known at compile time then it will always return the same result and may be a candidate for “invocation” at compile time.
The fact that the keywords both contain the string “const” is a consequence of the English language.
- nybble41 5y agoUntil C++14 constexpr on a non-static member function did actually imply const, since mutation was (logically enough) not permitted in constexpr functions. C++14 relaxed this restriction, however, by allowing constexpr functions to mutate objects which only exist within the lifetime of the constexpr expression, so constexpr no longer implies const.