Summary
This episode explores the seemingly contradictory combination of 'constexpr' and 'mutable' keywords in C++. Jason demonstrates that a lambda marked as both 'constexpr' and 'mutable' won't compile because 'constexpr' implies 'const', making the lambda uncallable. However, he shows that within a class, declaring specific member variables as 'mutable' allows them to be modified even when used in a 'constexpr' object. This creates an interesting use case where some values can be computed at compile time but remain modifiable at runtime, though Jason suggests 'consteval' might be a cleaner alternative for many scenarios.
Related C++ Standard Sections
This episode covers topics found in these sections of the C++ standard:
-
[dcl.constexpr]71% match -
[depr.static.constexpr]66% match -
[diff.cpp11.dcl.dcl]60% match