From: Lew on
On Jul 20, 10:18 am, Jorgen Grahn <grahn+n...(a)snipabacken.se> wrote:
> ["Followup-To:" header set to comp.lang.c++.  Neither the Java nor the
> comp.programming people want to read about const correctness, I'm sure.]
>

Don't be so sure. Java has 'final', which isn't exactly the same as
'const' but is similar and applies similarly to the "principle of
least privilege" and the safety thereof.

Both 'const' and 'final' express the intention to prevent change to a
variable's value.

--
Lew
From: Peter Duniho on
Lew wrote:
> On Jul 20, 10:18 am, Jorgen Grahn <grahn+n...(a)snipabacken.se> wrote:
>> ["Followup-To:" header set to comp.lang.c++. Neither the Java nor the
>> comp.programming people want to read about const correctness, I'm sure.]
>
> Don't be so sure. Java has 'final', which isn't exactly the same as
> 'const' but is similar and applies similarly to the "principle of
> least privilege" and the safety thereof.
>
> Both 'const' and 'final' express the intention to prevent change to a
> variable's value.

But, in C++ "const" can apply either to the variable itself, or to the
object to which the variable refers. Java's "final" applies only to the
variable. As such, "final" never has to propagate through method calls.

I think the closest thing Java has to the "const-correctness" issue in
C++ are checked exceptions. The main problem with both is the
difficulty in keeping all the code in sync.

But C++ has it a bit worse, because "const" is not required even where
applicable (never mind that it wasn't even around or in common use when
a lot of the APIs that C/C++ programs use were created). So you could
be in a context where "const" has been applied, you want to call
something that is still within the spirit of "const", but you have to
cast the "const" away because the thing you want to call didn't use "const".

And of course, once you've cast the "const" away, or even have that
capability, it ceases to be as useful as it was. It's more of a
"suggestion" than a real contract enforced by the compiler.

I'm a big fan of language constructs that constrain the code in certain
ways, from data/implementation hiding/encapsulation to things like
"const", "final", "readonly" (C#), etc. that help convey and,
especially, enforce intent. But these kinds of things really need to be
done in a way that doesn't allow the programmer to just wish the
restrictions away any time they like. Otherwise, it's too tempting to
do just that when the alternative is to spend hours or days updating the
code to use the restriction properly.

Pete
From: Joshua Maurice on
On Jul 20, 10:54 am, Peter Duniho <NpOeStPe...(a)NnOwSlPiAnMk.com>
wrote:
> I'm a big fan of language constructs that constrain the code in certain
> ways, from data/implementation hiding/encapsulation to things like
> "const", "final", "readonly" (C#), etc. that help convey and,
> especially, enforce intent.  But these kinds of things really need to be
> done in a way that doesn't allow the programmer to just wish the
> restrictions away any time they like.  Otherwise, it's too tempting to
> do just that when the alternative is to spend hours or days updating the
> code to use the restriction properly.

Unfortunately (or fortunately ?), this is C++, and the motto is we'll
give you tools to help you not shoot yourself in the foot, perhaps
even make them the default, but if you're dead set on shooting
yourself in the foot, C++ will allow you to do so.
From: Christian Hackl on
Peter Duniho ha scritto:

> But C++ has it a bit worse, because "const" is not required even where
> applicable (never mind that it wasn't even around or in common use when
> a lot of the APIs that C/C++ programs use were created). So you could
> be in a context where "const" has been applied, you want to call
> something that is still within the spirit of "const", but you have to
> cast the "const" away because the thing you want to call didn't use "const".

Maybe I'm just lucky, but I cannot even remember the last time I
actually had to use const_cast because of some broken (or legacy) C++ or
C API. How often do you really encounter this problem?


--
Christian Hackl
hacki(a)sbox.tugraz.at

Milano 2008/2009 -- L'Italia chiam�, s�!
From: Peter Duniho on
Christian Hackl wrote:
> Peter Duniho ha scritto:
>
>> But C++ has it a bit worse, because "const" is not required even where
>> applicable (never mind that it wasn't even around or in common use
>> when a lot of the APIs that C/C++ programs use were created). So you
>> could be in a context where "const" has been applied, you want to call
>> something that is still within the spirit of "const", but you have to
>> cast the "const" away because the thing you want to call didn't use
>> "const".
>
> Maybe I'm just lucky, but I cannot even remember the last time I
> actually had to use const_cast because of some broken (or legacy) C++ or
> C API. How often do you really encounter this problem?

I have successfully avoided using C++ to any significant extent for
nearly a decade. In the 90's, it came up on a regular enough basis to
dissuade me from making more than a half-hearted attempt to "const"-ify
my code.

Maybe it's not as big of a problem as it used to be.

Pete