...
In this noncompliant code example, the function g
() is passed a const int &, which is then cast to an int & and modified. Because the referenced value was previously declared as const
as const, the assignment operation results in undefined behavior.
...
This compliant solution uses the
keyword when declaring mutable
cachedValue
, which allows cachedValue
to be mutated within a const
context without triggering undefined behavior.
...