The solution to GotW #6b is now available:
GotW #6b Solution: Const-Correctness, Part 2 (updated for C ++11/14)
by Herb Sutter
From the article:
Option 1 is to use a mutex in the perhaps-soon-to-be-canonical “
mutable mutex mutables
” pattern:// Option 1: Use a mutex double get_area() const { auto lock = unique_lock<mutex>{mutables}; if( area < 0 ) // if not yet calculated and cached calc_area(); // calculate now return area; } private: // ... mutable mutex mutables; // canonical pattern: mutex that mutable double area; // covers all mutable membersOption 1 generalizes well if you add more data members in the future. However, it’s also more invasive and generalizes less well if you add more
Option 2 is to just changeconst
member functions in the future that usearea
, because they will all have to remember to acquire a lock on the mutex before usingarea
.double
tomutable atomic<double>
. ...
Add a Comment
Comments are closed.