Jump to page: 1 2
Thread overview
Does D support anonymous structs?
Feb 23, 2006
Sean Kelly
Feb 23, 2006
Derek Parnell
Feb 23, 2006
Derek Parnell
Feb 23, 2006
Alex Stevenson
Warning
Feb 23, 2006
Georg Wrede
Feb 23, 2006
Derek Parnell
Feb 23, 2006
Charles
Feb 23, 2006
James Dunne
Feb 24, 2006
MicroWizard
Mar 13, 2006
Walter Bright
Feb 23, 2006
Sean Kelly
February 23, 2006
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>
February 23, 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 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
February 23, 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
February 23, 2006
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
February 23, 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;
    }
};
February 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.
February 23, 2006
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
February 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.


February 23, 2006
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
February 24, 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.


« First   ‹ Prev
1 2