This week on SO:
Is a constexpr more “constant” than const?
The C++ Programming Language Fourth Edition - Bjarne Stroustrup: (emphasis mine)
2.2.3. Constants
In a few places, constant expressions are required by language rules (e.g., array bounds (§2.2.5, §7.3), case labels (§2.2.4, §9.4.2), some template arguments (§25.2), and constants declared using constexpr). In other cases, compile-time evaluation is important for performance. Independently of performance issues, the notion of immutability (of an object with an unchangeable state) is an important design concern (§10.4).
It seems that Stroustrup is suggesting here that
constexpr
ensures immutability of an object better than a traditionalconst
declaration. Is this correct? Are there ways in whichconstexpr
can be more secure/less volatile thanconst
, or does Stroustrup simply mean that since there are ways to useconstexpr
that are not supported withconst
(see Is constexpr really needed?), in those cases immutability can be ensured usingconstexpr
?
Add a Comment
Comments are closed.