Bypassing const with a union

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Jun 2 00:18:44 PDT 2012


On 02.06.2012 3:28, Era Scarecrow wrote:
> 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.
>

Mmm IRC you can assign each field in const constructor only once. After 
that it's cooked and treated as const from now on.

> 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.


Sure it is.

-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list