31 points · by cdmh · 13y ago · 35 comments
As you can guess it is about const and mutable, and Herb speaks about this feature(or problem -- depending on how you see it).
C++98: const == logically const
C++11: const == thread-safe
Also, I do not think the claim that const member functions cannot have side effects is correct.
For example, it is OK, but very bad practice to have a const member function update a global integer counter.
* what the compiler will enforce: this has nothing to do with thread safety, and will allow what you mention.
* what guarantees the standard library will give:
[17.6.5.9/3] A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s non-const arguments, including this.
This means that the standard library assumes that your const objects will be thread safe in order to guarantee thread-safety itself. Updating a global without synchronization is not thread-safe.
In this case the user of the library should be aware of which assumptions the library guarantees, not only what the current compiler does
You could also argue that read/write locks aren't legitimate applications (starvation, increased contention, alternate solutions).
class c { int m1; public: void set_m1( int i ) { m1 = i; } int calc( int i ) const { return m1 * 10; } };
calc is properly const, but it's not threadsafe.
1. Don't use global state 2. Don't use goto 3. Don't, for the love of all things good in this world, use exceptions to do control flow 4. Don't just pass strings around in your functions and deserialise them in your function 5. Make your data members private with public accessors and mutators.
How so? You cannot mutate a data member(without placing mutable before it) in const member function. It is a compiler error. Pretty explicit IMO.
The point of the article is that you should ensure thread-safety when you mutate mutable(sounds funny, I know) members in const methods.
The C++ language is totally unsafe, so when you call third-party code, literally anything can happen.
Just because there might be a convention for "const" methods to be thread-safe doesn't mean that they are actually implemented this way, regardless of what the language spec says.
It's alarming that this guy apparently writes books, hopefully no one reads them.
This was confusing to read in the context of C++, because I typically associate that view with functional programming. Don't the majority of C++ classes require mutable data to work properly, even the ones in the STL? How do you write std::vector<> without mutable data?
Ok, so I looked a bit more, and it turns out there's a "mutable" keyword...
If you used const correctly, you couldn't change anything about the class, therefore it was also thread-safe. If your class had mutable members or got around the const guarantee through backdoors (e.g. pointer passed in that modified another object, or member pointers that called non-const methods), it wasn't correctly const and shouldn't have been marked as such.
There is nothing wrong with the const keyword, just with programmers who marked things const that broke the const contract. There is no language ambiguity on this.
It's a shame that this is more best practice. Hopefully the specification will address this in the future that you can only call const from const, and cast from const to non-const can't be used.
int x;
int const *cpx = &x;
int *px = const_cast<int *>(cpx);
*px = 1; // defined behavior, modifying non-const object through non-const pointer
While this may seem ok here, consider that the same applies to functions taking pointers to const. While this usually means the function won't modify the pointed-to objects, there is no guarantee from the language about that.Pretty much anything that breaks the contract is readily apparent when reading/searching the code, and indicates that something needs to be rewritten.
Just because you can break the contract doesn't mean you should break the contract. Const correctness produces more readable/easily understood code.
Don't be lazy, and make life better for anyone that has to maintain and use the code after you.
Even if things like 'const' are useless to the compiler in optimisation terms, they provide (weak) contracts for the humans writing the code, and obvious flags when these contracts are broken, by having to be explicit about, in this instance, const_cast's. ('git grep const_cast' => places to examine closely for bugs.)
I only noticed this problem when I learned how languages such as Smalltalk/objective-c deal with the same issue: they create different classes for each situation, so if you want a mutable array you have to say so.