From slices to perfect imitators: opByValue

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Thu May 8 09:02:36 PDT 2014


On Thursday, 8 May 2014 at 15:39:24 UTC, Sönke Ludwig wrote:
> Unless I'm completely mistaken, it's safe to cast away const 
> when it is known that the original reference was constructed as 
> mutable.

Depends. If the original type referencing the (originally 
mutable) allocated data happens to be *im*-mutable, then it 
*would* be illegal to modify it, even if you *can*.

EG:

immutable myFirstRC = RefCounted!int(1);
immutable myReference = myFirstRC;

In this particular case, you'd be modifying a ref count that can 
only be accessed via an immutable pointer. As such, the compiler 
is free to assume the value has never been changed, and avoid 
reading it all together, destroying your payload at the end of 
"myFirstRC"'s life-cycle.

I honestly don't think (given D's transitive constness 
mechanics), that a const RefCount *could* make any sense.

--------

If you have const data referencing mutable data, then yes, you 
can cast away all the const you want, but at that point, it kind 
of makes the whole "const" thing moot.


More information about the Digitalmars-d mailing list