> On 8/1/2011 5:07 PM, Jonathan M Davis wrote: > > You do get some gains from const in C++, but it's pretty much all gained > > by convention. It _is_ better than just comments in that the compiler > > will complain if you break the convention, forcing you to explicitly > > break it (e.g. via casting or mutable), but ultimately, it doesn't > > enforce much. I do believe that it enforces enough that it's worth > > having, but it obviously doesn't provide the guarantees that D's const > > does. > > Any code at any time can modify a mutable member of a const object, without > even bothering with the nicety of a cast. The notion that C++ "supports" > logical const is without merit.
If an object isn't const, you can't call anything on it that isn't const. That does gain you some level of safety which is enforced by the compiler. But since you can completely side step it via a cast (or by having a mutable member variable), you don't really get any guarantees. However, that minimal support _is_ still better than nothing IMHO. So, C++ supports const in that sense. But yes. You're right that logical const isn't much more than a convention. The fact that C++ has mutable helps support the convention, but it's still ultimately a convention. Still, the fact that the compiler does have some level of enforcement for const does add something of value. - Jonathan M Davis
