How to strip struct/class invariants?

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 6 01:44:33 PDT 2015


On Sunday, 5 July 2015 at 15:39:50 UTC, Artem Tarasov wrote:
> On Sunday, 5 July 2015 at 14:44:30 UTC, John Colvin wrote:
>>
>> struct A
>> {
>>     ubyte[B.sizeof] mem;
>>     @property ref B b()
>>     {
>>         return *cast(B*)(mem.ptr);
>>     }
>>     mixin std.typecons.Proxy!b;
>> }
>>
>
> Thanks, I followed your suggestion and effectively rolled out 
> my own union implementation. Ugly but it works.
>
> struct A
> {
>     ubyte[maxSizeof] _data;
>     @property ref T _as(T)() inout { return 
> *cast(T*)(_data.ptr); }
>     alias b = _as!uint;
>     alias c = _as!size_t;
>     alias d = _as!double;
> }

That works, but what I meant was, using my definition of struct 
A, you can put it in a union without the compiler complaining, 
and the invariants still get called. No need to write your own 
union implementation.


More information about the Digitalmars-d-learn mailing list