Yes, use mutable. It is intended for this purpose: where is the entire context of the const function (i.e., an Accessor or some other action that is logically readable.), But where some element of write access is needed for the mutex or reference counter, etc. .
The function must be const, even if it blocks the mutex within itself. This makes the code neutral, without having to reveal the details that I believe is what you are trying to do.
There are very few places where const_cast<> should be legally used, and this is not one of them. Using const on an object, especially in a const function, is a nightmare for serving code. Consider:
token = strtok_r( const_cast<char*>( ref_.c_str() ), ":", &saveptr );
Actually, I would say that when you see const_cast in a const function, you should start by saying that the function is not const (very soon after you have to get rid of const_cast and execute the const function again) / p>
Component 10
source share