Bypassing const with a union

Era Scarecrow rtcvb32 at yahoo.com
Fri Jun 1 16:28:29 PDT 2012


On Friday, 1 June 2012 at 23:14:14 UTC, Dmitry Olshansky wrote:
> There is also cast() that just cancels out all 
> const/shared/immutable.

  Only the first level, transitive const/immutable don't go away 
in my experience. Perhaps I'm doing it wrong, or perhaps it's 
just a protective feature to protect the lower levels so you 
don't get C++'s const system.

>> Breaking the const system while your still building/preparing 
>> the new object should be allowed (as with the slice example)
>
> Yes in constructor. Or by constructing incrementally a mutable 
> object, inside pure function e.g. compiler can convert to 
> immutable on return (auto-magically).

  Which is sometimes where I'm getting stuck. In the constructor 
it complains about not convertible from const to mutable even if 
the object being passed back will be const/immutable.

  In my limited experience where it is emulating a slice I would 
need an exact copy of the struct and then modify what I need 
before passing it back; cast doesn't do the job, and manually 
copying const objects to non-const is an annoyance or a pain in 
it's own regard.



More information about the Digitalmars-d-learn mailing list