www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Does D support anonymous structs?

reply Sean Kelly <sean f4.ca> writes:
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
parent reply Derek Parnell <derek psych.ward> writes:
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 aggregate
Yes. 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
next sibling parent reply Derek Parnell <derek psych.ward> writes:
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
next sibling parent Alex Stevenson <ans104 cs.york.ac.uk> writes:
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; } };
Feb 23 2006
prev sibling next sibling parent reply Georg Wrede <georg.wrede nospam.org> writes:
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
next sibling parent "Derek Parnell" <derek psych.ward> writes:
On Fri, 24 Feb 2006 02:48:38 +1100, Georg Wrede <georg.wrede nospam.org>  
wrote:

 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.
Sorry, but maybe I should have also mentioned ... alias char Foo; <g> -- Derek Parnell Melbourne, Australia
Feb 23 2006
prev sibling next sibling parent "Charles" <noone nowhere.com> writes:
 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
prev sibling next sibling parent James Dunne <james.jdunne gmail.com> writes:
Georg Wrede wrote:
 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.
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 Dunne
Feb 23 2006
prev sibling parent MicroWizard <MicroWizard_member pathlink.com> writes:
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
prev sibling parent "Walter Bright" <newshound digitalmars.com> writes:
"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
prev sibling parent Sean Kelly <sean f4.ca> writes:
Derek Parnell wrote:
 On Wed, 22 Feb 2006 20:58:35 -0800, Sean Kelly wrote:
 ------------------------------------

 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.
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
Feb 22 2006