Do we really need const?

Walter Bright newshound1 at digitalmars.com
Mon Sep 17 23:13:16 PDT 2007


Robert Fraser wrote:
> I'd like to pose a question to those who have used C++'s const: do
> you feel that it has saved more time by preventing bugs than it has
> taken by being forced to type it all the time, and the time spent
> when it has to be removed all throughout a hierarchy, as inevitably
> has to happen at least once? That is, const-correctness is a time
> investment, so do you feel that investment has paid off for you?

At the upcoming http://www.astoriaseminar.com, I think I'll do some 
asking around on this issue. There'll be a lot of C++ diehards there.


> I can say that working in Java, I have _never_ felt that if I pass a
> class reference that was "constant" in nature to a method written by
> somebody else or even to entirely different subsystem, that the
> invariantness contract, specified only in the documentation, would be
> broken.

Some Java professionals have reported problems with not being able to 
specify const classes. These people work on Java programs with large teams.

> Compiler checks in that case end up being as useless and
> annoying as checked exceptions.

I don't think that's the same issue. The problem with checked exceptions 
is that suppose you have functions A, B, C, where A calls B, and B calls 
C. Now, you throw a new exception in C, and catch it in A. Arggh, you've 
got to change B.




More information about the Digitalmars-d mailing list