How to strip struct/class invariants?

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 5 07:44:28 PDT 2015


On Sunday, 5 July 2015 at 12:15:32 UTC, Artem Tarasov wrote:
> OK, so there was an old bug fixed in 2.067 
> (https://issues.dlang.org/show_bug.cgi?id=4421) so that now 
> unions apparently can't contain a struct that has invariants. 
> It kinda makes sense, although I don't see why the invariants 
> can be simply ignored, as they don't have as much importance as 
> destructors/postblits.
>
> But more to the point. I have a struct that has an invariant, 
> and I wish to use it as a member of an union. With the latest 
> compiler, I have to somehow remove the invariant. Is there some 
> compile-time magic that I can use for this?

Not perfect, but I think you can do:

struct A
{
     ubyte[B.sizeof] mem;
     @property ref B b()
     {
         return *cast(B*)(mem.ptr);
     }
     mixin std.typecons.Proxy!b;
}

where B has an invariant. Even better, the invariant should still 
get called.


More information about the Digitalmars-d-learn mailing list