April 4th, 2011, 4:39 am
QuoteMember function is an interface contract. Therefore we have to ask ourselves what guarantee the caller can get from a const function declaration? In a multi-thread world, two consecutive calls to the same const function may return completely different results because another thread may sneak in between and change something. Even during the same call, the callee's states may still get changed because of another thread. So what does "const-ness" mean to the caller?That is why function objects are copied by PPL/TBB parallel patterns. Each thread get's it is own local copy. Two or more threads won't use the same copy. Constness guarantees that all copies behave identically just like the original, regardless of how many copies are created or in what order the copes are created. It is like a meter stick - we don't care when and where it was manufactured as long as it is equivalent to the master copy.QuoteFurthermore, an interface contract just needs to tell the caller what service it provides, not how the service will be provided. An object's internal state is its own business, i.e. encapsulation. I agree that a class' internal members can be classified as regular and scratch (i.e. mutable). But they both should be internal and invisible to the outside world. So why we want to promise others one member function will not change some (regular) members and may change some (mutable) members? Again, why the caller needs to know this and what tangible benefit the caller can get by knowing this?Constness is part of the interface. It tells the caller that the outcome depends only on the value of supplied arguments, not on the argument values used in previous calls or how many times the object was copied or used. Generating random bits from RNGs shouldn't be const, but Inverse CDF can be and should be provided by a const member function. It is not OK to pass RNGs to PPL/TBB parallel patters (at least not without explicit synchronization and/or copy control), but Inverse CDF can be duplicated so that each thread can use its own copy without synchronization. Mutable variables are rather obscure C++ feature. They are meant to store scratch data only (e.g. for performance purposes). The outcome of function calls shouldn't depend on scratch data. Mutable variables should be completely invisible to to the caller.QuoteIf declaring const-ness member function is about self-discipline, keep it to ourselves and don't make over-commitment to others. This is what const member variable does. Declaring a member variable const is simply a reminder to us that we plan to only use this member variable but not manipulate it.It is not about self-discipline only. Constness is type property and checked at compile time. It will prevent us from passing RNGs to PPL/TBB. Again, it is the protection against Murphy, not Machiavelli.QuoteTo me, it's quite useful and help to compare the features of different languages. And honestly I trust those Java and .NET designer are very experienced and must have spent lots of thinking when deciding which feature to drop and to add. Features like generic has built-in conflict with language execution security (which is very important to Java and .NET in the internet age). But for features like const member function and operator overloading, I don't see any strict show stops that prevent them from being included. So I would guess they are purposely excluded.Java (and .Net as well) design was an incremental process (just like anything else that big and complex). The way Java used today isn't necessarily the same as it was initially intended. Java was meant to be something intermediate between C++ and scripting languages: less fine grained control than C/C++ but still compiled and static/strongly typed. But over time Java moved closer to C++ and further from the languages used for rapid development (flash, python, etc). Const member functions isn't something rapid development should bother with, so Java didn't support them. But constness isn't backward compatible. Can't go back and make something const if it already being used in many places.
Last edited by
renorm on April 3rd, 2011, 10:00 pm, edited 1 time in total.