diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md
index ff6bd38..3468411 100644
--- a/CppCoreGuidelines.md
+++ b/CppCoreGuidelines.md
@@ -10435,7 +10435,7 @@ for example.)
Flag C-style and functional casts.
-## ES.50: Don't cast away `const`
+### ES.50: Don't cast away `const`
##### Reason
@@ -10451,19 +10451,31 @@ Such examples are often handled as well or better using `mutable` or an indirect
Consider keeping previously computed results around for a costly operation:
+ int compute(int x); // compute a value for x; assume this to be costly
+
+ class Cache { // some type implementing a cache for an int->int operation
+ public:
+ pair find(int x) const; // is there a value for x?
+ void set(int x, int v); // make y the value for x
+ // ...
+ private:
+ // ...
+ };
+
class X {
public:
- int get_val(int x)
+ int get_val(int x)
{
- if (auto p = cache.find(x)) return ->second;
- int val = compute(val);
- cache[x] = val;
+ auto p = cache.find(x);
+ if (p.first) return p.second;
+ int val = compute(x);
+ cache.set(x,val); // insert value for x
return val;
}
// ...
private:
- map cache;
- }
+ Cache cache;
+ };
Here, `get_val()` is logically constant, so we would like to make it a `const` member.
To do this we still need to mutate `cache`, so people sometimes resort to a `const_cast`:
@@ -10472,52 +10484,57 @@ To do this we still need to mutate `cache`, so people sometimes resort to a `con
public:
int get_val(int x) const
{
- if (auto p = cache.find(x)) return ->second;
- int val = compute(val);
- const_cast