Suggestion: class/struct tuples preserve anonymous unions/structs

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Feb 14 19:10:02 PST 2007


If you write something like this:

struct S
{
    int x;

    union U
    {
        int a;
        int b;
    }

    U u;
}

And print out its field types:

foreach(T; FieldTypeTuple!(S))
    writefln(typeid(T));

It will print out

int
test.S.U

But if you make the union anonymous:

struct S
{
    int x;

    union
    {
        int a;
        int b;
    }
}

The output becomes:

int
int
int

The anonymous union is getting "flattened out" by the type tuple mechanism.

This messes up some automation features.  For example I'm trying to write a 
simple "Serialize" function which can serialize entire structs.  But unions 
can't be automatically serialized, since the Serialize function has no idea 
which member of the union is currently "valid."  So I have error checking to 
disallow serializing unions, but since the .tupleof facility flattens out 
anonymous unions, it can't check for errors (and therefore I don't know that 
there's a problem until I write the struct out to a file and end up with a 
bunch of invalid members, since they came from the anonymous union).

I'm not sure how anonymous unions and structs are handled by the compiler. 
If they are created as "secret" types, would it then be possible for the 
above struct with the anonymous union to consist of:

int
test.S.__UNION0

or something like that? 





More information about the Digitalmars-d mailing list