Does D support anonymous structs?
Alex Stevenson
ans104 at cs.york.ac.uk
Thu Feb 23 01:52:58 PST 2006
Derek Parnell wrote:
> On Thu, 23 Feb 2006 17:26:13 +1100, Derek Parnell wrote:
>
>
>>With anonymous structs, the definition *is* the declaration.
>
>
> Sorry, but I forgot something nice. Anonymous structs don't seem to be
> solving any problem that I've come across, but anonymous unions are very
> nice.
>
> struct S
> {
> int type;
> union {
> int i;
> long l;
> short s;
> real r;
> float f;
> Foo foo;
> }
> }
>
> S s;
> s.type = 1;
> s.i = 88;
>
> ...
> s.type = 3;
> s.r = 88.98;
>
>
I use anonymous structs because in my code there's an anonymous union of
anonymous structs - It's not the nicest of ways to do it, but it works
and I like the anonymousness of it - in C I'd probably just give up and
accept the extra memory cost of having redundant struct values because C
always annoys me when I have complex struct-union-struct hierarchies -
args.args.operand1.bytearg etc.
struct opcode_args
{
union
{
struct
{
union
{
byte bytearg;
ubyte ubytearg;
short shortarg;
ushort ushortarg;
uint regArg1;
}
union
{
ushort ushortarg2;
byte bytearg2;
ubyte ubytearg2;
short shortarg2;
uint regArg2;
}
union
{
ubyte ubytearg3;
ushort ushortarg3;
}
}
struct table_s
{
uint regArg;
uint[] entries;
}
table_s table;
struct lookup_s
{
uint regArg;
uint defaultjump;
tableentry[] entries;
}
lookup_s lookup;
}
};
More information about the Digitalmars-d
mailing list