digitalmars.D - Does D support anonymous structs?
- Sean Kelly (34/34) Feb 22 2006 C:\code\d>type test.d
- Derek Parnell (43/70) Feb 22 2006 The above doesn't work because if you have an anonymous struct/union is
- Derek Parnell (30/31) Feb 22 2006 Sorry, but I forgot something nice. Anonymous structs don't seem to be
- Alex Stevenson (51/83) Feb 23 2006 I use anonymous structs because in my code there's an anonymous union of...
- Georg Wrede (7/23) Feb 23 2006 Of course you know, and all regulars here know, but the casual reader
- Derek Parnell (7/27) Feb 23 2006 Sorry, but maybe I should have also mentioned ...
- Charles (3/27) Feb 23 2006 Hmm Ive never heard ( or done ) this, why is that ?
- James Dunne (13/40) Feb 23 2006 Oh that's nothing - this one time, at home, I serialized pointers to
- MicroWizard (4/27) Feb 24 2006 Really nice ;-)))
- Walter Bright (3/6) Mar 12 2006 Anonymous structs are useful as union members.
- Sean Kelly (5/29) Feb 22 2006 Thanks. I ran into this porting C code, which allows anonymous named
C:\code\d>type test.d void main() { struct { int i; } s; s.i = 1; } C:\code\d>dmd test test.d(3): anonymous struct can only be a part of an aggregate test.d(3): undefined identifier s test.d(3): TOK117 has no effect in expression (s) test.d(4): undefined identifier s test.d(4): no property 'i' for type 'int' test.d(4): constant s.i is not an lvalue C:\code\d> ------------------------------------ C:\code\d>type test.d void main() { struct S { union { struct { int i1; } s1; } u; } S s; } C:\code\d>dmd test test.d(10): no identifier for declarator s1 test.d(11): no identifier for declarator u C:\code\d>
Feb 22 2006
On Wed, 22 Feb 2006 20:58:35 -0800, Sean Kelly wrote:C:\code\d>type test.d void main() { struct { int i; } s; s.i = 1; } C:\code\d>dmd test test.d(3): anonymous struct can only be a part of an aggregateYes. This is the rule "can only be a part of an aggregate".------------------------------------ C:\code\d>type test.d void main() { struct S { union { struct { int i1; } s1; } u; } S s; }The above doesn't work because if you have an anonymous struct/union is *must not* have a name - that's why its anonymous. Consequently, you can only have one anonymous aggregate per parent aggregate. void main() { struct S { union { struct { int i1; }; }; } S s; s.i1 = 42; // You refer to anonymous struct members // by the member id directly. } With named stucts, you have to separate the definition and the declaration. struct S // Definition { int i1; } S s; // Declaration s.i1 = 42; With anonymous structs, the definition *is* the declaration. struct S { int i1; struct { int i2; } // Both definition and declaration. } S s; s.i1 = 86; s.i2 = 99; -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 23/02/2006 5:19:12 PM
Feb 22 2006
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; -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 23/02/2006 5:32:34 PM
Feb 22 2006
Derek Parnell wrote:On Thu, 23 Feb 2006 17:26:13 +1100, Derek Parnell wrote: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; } };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;
Feb 23 2006
Derek Parnell wrote: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; } }Of course you know, and all regulars here know, but the casual reader has to be warned here. NEVER MIX VALUE AND REFERENCE types in a union! While technically it poses no problem, in real life you'll end up shooting yourself in the foot -- faster than it takes to say 'Ouch!' And if you don't, the later maintainer of your code *will* shoot you.
Feb 23 2006
On Fri, 24 Feb 2006 02:48:38 +1100, Georg Wrede <georg.wrede nospam.org> wrote:Derek Parnell wrote:Sorry, but maybe I should have also mentioned ... alias char Foo; <g> -- Derek Parnell Melbourne, AustraliaSorry, 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; } }Of course you know, and all regulars here know, but the casual reader has to be warned here. NEVER MIX VALUE AND REFERENCE types in a union! While technically it poses no problem, in real life you'll end up shooting yourself in the foot -- faster than it takes to say 'Ouch!' And if you don't, the later maintainer of your code *will* shoot you.
Feb 23 2006
NEVER MIX VALUE AND REFERENCE types in a union!Hmm Ive never heard ( or done ) this, why is that ? "Georg Wrede" <georg.wrede nospam.org> wrote in message news:43FDD956.5020603 nospam.org...Derek Parnell wrote: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; } }Of course you know, and all regulars here know, but the casual reader has to be warned here. NEVER MIX VALUE AND REFERENCE types in a union! While technically it poses no problem, in real life you'll end up shooting yourself in the foot -- faster than it takes to say 'Ouch!' And if you don't, the later maintainer of your code *will* shoot you.
Feb 23 2006
Georg Wrede wrote:Derek Parnell wrote:Oh that's nothing - this one time, at home, I serialized pointers to disk! =P And this other time, at work, I wrote a Delphi program to serialize a record right from memory out to the disk! (It wasn't packed, and no pointers) -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O M-- V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e h>--->++ r+++ y+++ ------END GEEK CODE BLOCK------ James DunneSorry, 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; } }Of course you know, and all regulars here know, but the casual reader has to be warned here. NEVER MIX VALUE AND REFERENCE types in a union! While technically it poses no problem, in real life you'll end up shooting yourself in the foot -- faster than it takes to say 'Ouch!' And if you don't, the later maintainer of your code *will* shoot you.
Feb 23 2006
Really nice ;-))) Should be collected in D.Learn (not only) for beginners. Tamas Nagy In article <43FDD956.5020603 nospam.org>, Georg Wrede says...Derek Parnell wrote: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; } }Of course you know, and all regulars here know, but the casual reader has to be warned here. NEVER MIX VALUE AND REFERENCE types in a union! While technically it poses no problem, in real life you'll end up shooting yourself in the foot -- faster than it takes to say 'Ouch!' And if you don't, the later maintainer of your code *will* shoot you.
Feb 24 2006
"Derek Parnell" <derek psych.ward> wrote in message news:1solrza5jdrll.1u7rbehl41qd4$.dlg 40tude.net...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.Anonymous structs are useful as union members.
Mar 12 2006
Derek Parnell wrote:On Wed, 22 Feb 2006 20:58:35 -0800, Sean Kelly wrote:Thanks. I ran into this porting C code, which allows anonymous named structs like the above. I was hoping I wouldn't have to invent one-off names for the structs, but it's not a big deal either way. Sean------------------------------------ C:\code\d>type test.d void main() { struct S { union { struct { int i1; } s1; } u; } S s; }The above doesn't work because if you have an anonymous struct/union is *must not* have a name - that's why its anonymous. Consequently, you can only have one anonymous aggregate per parent aggregate.
Feb 22 2006