TITLE: casting away const (Newsgroups: comp.lang.c++.moderated) PROBLEM: pronet03@indirect.com (Paul Carlton) [...] it would be interesting to get other people's feedback on the problem of having 'const' cast away (explicitly and more disturbingly 'implicitly'). [...] RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 3 May 96 Casting away const is valid if the actual object is not declared const, but has undefined results if the actual object is declared const. For example, if a function parameter is declared const T* // or const T& it means that the function is not expected to change the pointed-to actual argument. It does not mean that the pointed-to arg is in fact const. The danger in casting away const is that in general you don't know whether the actual object is const or not. In addition, you are telling a lie if you claim that the function won't change the object when it really does. The calling location might depend on the object not changing. In this example, casting away const is almost certainly a bad idea for those two reasons. A more legitimate use for casting away const is to differentiate between "logical const" and "bitwise const". A class type might contain private data which does not affect the observable behavior of the class or its abstraction. (A cache of some sort to speed up processing, for example, or statistics on class usage for later analysis.) In this case, you might declare const member functions which do in fact change the hidden data (some bits change), but which don't change anything that a class client would care about (the object is logically unchanged). A const member function is not allowed to modify any class data, so you would have to cast away const inside that member function. The draft C++ standard adds a new keyword "mutable" for use on class data members which means that the data member can be modified even if the object or a member function is declared const. The language extension effectively removes the need to cast away const, and makes the program safer. Example: class Myclass { public: Myclass(); int get() const; // returns something interesting private: int used; //usage count for analyzing class behavior }; const Myclass m; cout << m.get(); // 'get' increments 'used' What's going on here? Object m is declared const, so in principle the compiler is allowed to place it in write-protected memory. What will happen in that case if 'get' casts away const so as to modify 'used'? Formally, the results are undefined. In practice compilers don't usually put an object in write-protected memory if the object has a constructor or destructor. This example is still going to be shaky. But let's make use of 'mutable': class Myclass { public: Myclass(); int get() const; // returns something interesting private: mutable int used; //usage count for analyzing class behavior }; Now the compiler knows that even a const Myclass object can be modified, and will not put it in write-protected memory. Further, member functions do not need to cast away const in order to change 'used'. Function 'get' can be written like this: int Myclass::get const { ++used; // instead of ++((Myclass*)this)->used; return something interesting; } This addition to C++ makes safe what is probably the only legitimate use for casting away const. You had some suggestions along the lines of adding bits to objects to keep track of constness. An implementation is allowed to do that already. (The "undefined" results of trying to modify a const object allow providing a diagnostic, for example.) But requiring such an addition in the language definition would add considerable overhead to all programs, even those that didn't care about const, and would break data compatibility with C. (The size or range of a basic type would be different in C and C++.) Under the current rules, we have a safe way to modify a const class object when it is legitimate to do so. Probably you should not ever cast away const if your compiler implements "mutable". If you still feel you need to cast away const, you can add the const checking yourself to those objects.