From Jason Turner
Watch on YouTube

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.

Resources

Tags