Logical const
Steven Schveighoffer
schveiguy at yahoo.com
Mon Nov 29 07:13:01 PST 2010
On Sun, 28 Nov 2010 16:27:20 -0500, Walter Bright
<newshound2 at digitalmars.com> wrote:
> Peter Alexander wrote:
>> Solves the problem, but now I've created a new one: getInverse now has
>> complete write access to my matrix, so when I do something as harmless
>> as:
>> Matrix inv = getInverse(myMatrix);
>> This innocent call has now lost the guarantee that myMatrix will come
>> out unmodified.
>
> Carefully examining C++ const reveals that it offers no protection at
> all against legally modifying a supposedly const type. It's so bad that
> the DMC++ optimizer completely ignores const, and relies on data flow
> analysis instead.
>
> Const in C++ offers some level of type checking, but beyond that it's
> more of a convention than a static guarantee. In fact, "logical
> constness" is a fraud anyway because the underlying data isn't constant
> at all, one is completely relying on convention. There's nothing at all
> preventing a supposedly logical-const-correct function from returning a
> different value every time it is called, and no way for the compiler to
> detect this.
This is complete BS. logical const in C++ is not the same as what logical
const in D would be. The transitive guarantee is much more to credit for
allowing optimizations and static analysis than the omission of mutable
members. How does the fact that an Object's monitor is mutable affect the
optimizer?
In fact, how can *any* const or immutable function be optimized any
differently than a mutable function? The only optimizations I can think
of are for pure functions. And a pure function with immutable becomes
significantly less optimizable if some fields might be mutable. Couple
that with the fact that a class may define new mutable members, it would
be impossible to statically prove whether a class is completely immutable
or logically immutable.
But it would be good enough to allow logically-const/immutable to be
different than const/immutable. The problem is that this severely
complicates the const system.
If you are going to argue this case, please do not use C++ as a straw man,
it's not the same as D.
-Steve
More information about the Digitalmars-d
mailing list