A const use-case I would NOT like to see replicated

James Dennett jdennett at acm.org
Fri Jun 22 23:16:50 PDT 2007


Walter Bright wrote:
> Tim Keating wrote:
>> I have been following the discussion on const/invariant/final, and
>> while I understand the need to add this feature to the language, my
>> initial reaction was repugnance. In order to add value and not just
>> fan a flame-war, I spent some time ruminating on why this is so. As a
>> result, I recalled a use case for const in C++ that caused me
>> considerable pain, and so I throw it out here for discussion.
> 
> I think you've run into the well known problem that you cannot do
> const-correctness halfway. Once you start using const, it has to be used
> throughout your program or library.
> 
> A corollary is it is not very practical to have a const-correct program
> that calls a library that is not const-correct. The general conclusion is:
> 
> 1) If you're writing a general purpose library, you have to make it
> const-correct, otherwise your users will curse you and your little dog,
> too.
> 
> 2) If you're writing a program, you can successfully ignore const. But
> if you do start using const, you'll find you have to commit to using it
> throughout.
> 
> As part of 2.0, I therefore had to go through Phobos to make it
> const-correct. I found a couple of cases where I didn't remember if an
> input value was modified or not, and had to spend a non-trivial amount
> of time figuring it out. This gave me a bit of a better appreciation for
> the documentation benefits of const-correctness.
> 
> Another point of note - I didn't find a single bug in Phobos from doing
> the conversion.

Yes you did; you documented one above (unless you consider
documentation defects not to be "bugs")!

-- James



More information about the Digitalmars-d mailing list