Logical const

Walter Bright newshound2 at digitalmars.com
Sun Nov 28 13:27:20 PST 2010


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.

D's const is meant to provide static guarantees, not an unverifiable convention.


More information about the Digitalmars-d mailing list