To help LDC/GDC

Timon Gehr timon.gehr at gmx.ch
Tue Apr 9 02:57:34 PDT 2013


On 04/09/2013 05:30 AM, Walter Bright wrote:
> On 4/8/2013 5:39 AM, Manu wrote:
>> But D makes no further guarantee. I don't see how const in D is any
>> different
>> than const in C++ in that sense? That's basically the concept of
>> const, it's not
>> a useful concept for optimisation, only immutable is.
>
> In C++, it is legal to cast away const and mutate it. That is undefined
> behavior in D.
>
> A D compiler can assume, for example, that a const reference passed to a
> pure function will not mutate that reference, nor anything transitively
> referred to by that reference. No such assumption can be made like that
> in C++.

The back end can assume this only if the DMD front end does its 
homework. It doesn't, probably because the spec does not formalize the 
type checking rules. There are plenty cases where a pure function can 
mutate something transitively referenced by some argument in @safe code, 
even if all arguments are qualified const.

eg. http://d.puremagic.com/issues/show_bug.cgi?id=9149




More information about the Digitalmars-d mailing list