Suggestion: class/struct tuples preserve anonymous unions/structs

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Feb 15 05:40:09 PST 2007


"Frits van Bommel" <fvbommel at REMwOVExCAPSs.nl> wrote in message 
news:er1cc3$3sr$1 at digitalmars.com...
>
> I don't think it's being "flattened out" by the type tuple mechanism, I 
> think it happens *before* that. They're not created as secret types, they 
> just seem to use different rules for offset calculation.
> I'm not really sure whether or not this is a bug in the compiler, but it 
> happens.

I was kind of worried about that.  Well we can hope..

> template validate(T, size_t Idx = 0) {
>     static assert(!is(T == union), T.stringof ~ " is a union itself!");
>     static assert(!is(typeof(T.tupleof)[Idx] == union),
>         "Member " ~ Idx.stringof ~ " of " ~ T.stringof ~ " is a union");
>
>     static if (Idx + 1 < T.tupleof.length) {
>         // check overlap caused by anonymous union members
>         static assert(T.tupleof[Idx].offsetof + T.tupleof[Idx].sizeof <= 
> T.tupleof[Idx + 1].offsetof,
>             "Member " ~ Idx.stringof ~ " of " ~ T.stringof ~ " overlaps 
> the next member");
>         // and check the other members
>         mixin validate!(T, Idx + 1);
>     }
>
>     static if (Idx < T.tupleof.length) {
>         // Recurse into member structs
>         static if (is(typeof(T.tupleof)[Idx] == struct))
>             mixin validate!(typeof(T.tupleof)[Idx]);
>
>         // uncomment these lines to recurse into member class references 
> as well:
>         //else static if (is(typeof(T.tupleof)[Idx] == class))
>         //    mixin validate!(typeof(T.tupleof)[Idx]);
>     }
> }

Thanks for this :)  I was thinking it might be possible to check the 
.offsetofs. 





More information about the Digitalmars-d mailing list