www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Anonymous unions

reply Derek Parnell <derek psych.ward> writes:
I think the code below is supposed to compile...

struct S
{
   union Anon
   {
      int Int;
      real Atom;
   }
}

void main()
{
    S test;
    test.Anon.Int = 2;  // line #13
}

But I get this message ...

  test.d(13): need 'this' to access member Int


I need to change it to this to get it to compile ...

struct S
{
   union Anon
   {
      int Int;
      real Atom;
   }
   Anon A;
}

void main()
{
    S test;
    test.A.Int = 2;
}

-- 
Derek
Melbourne, Australia
1/06/2005 1:02:30 PM
May 31 2005
next sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Derek Parnell wrote:
 I think the code below is supposed to compile...
 
 struct S
 {
    union Anon
    {
       int Int;
       real Atom;
    }
 }
 

I think anonymous unions have no names (hence anonymous) So strictly speaking, the union above is probably not anonymous. This should probably compile: struct S { union { int Int; real Atom; } }
 void main()
 {
     S test;
     test.Anon.Int = 2;  // line #13
 }
 
 But I get this message ...
 
   test.d(13): need 'this' to access member Int

Anon is the type name, not an instance. I think that's why it needs a "this" (i.e. an instance)
 
 
 I need to change it to this to get it to compile ...
 
 struct S
 {
    union Anon
    {
       int Int;
       real Atom;
    }
    Anon A;
 }
 
 void main()
 {
     S test;
     test.A.Int = 2;
 }
 

This (I think) is c++'s way of doing it .. it's definitly not anonymous.
May 31 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 31 May 2005 21:12:14 -0600, Hasan Aljudy wrote:

 Derek Parnell wrote:
 I think the code below is supposed to compile...
 
 struct S
 {
    union Anon
    {
       int Int;
       real Atom;
    }
 }
 

I think anonymous unions have no names (hence anonymous) So strictly speaking, the union above is probably not anonymous. This should probably compile: struct S { union { int Int; real Atom; } }

Thanks. Yes it does. But I guess it means only one anonymous union per struct though ;-) -- Derek Melbourne, Australia 1/06/2005 1:26:13 PM
May 31 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Derek Parnell wrote:
 But I guess it means only one anonymous union per struct though ;-)

Actually no, no it doesn't.. I just tested with this: # import std.stdio; # # struct S { # int Si; # # union { # int Ai; # float Af; # } # # union { # int Bi; # float Bf; # } # } # # void main() { # S s; # # s.Ai = 123; # s.Bf = 3.14; # # writefln("s.Ai : ", s.Ai); # writefln("s.Bf : ", s.Bf); # # s.Af = 4.2; # # writefln("s.Af : ", s.Af); # writefln("s.Bf : ", s.Bf); # } Although granted, naming might be interesting when having multiple anonymous unions. -- Chris Sauls
May 31 2005
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Chris Sauls wrote:
<snip>
 Although granted, naming might be interesting when having multiple 
 anonymous unions.

An anonymous union doesn't create a scope. And so it's illegal for names to collide between them. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 01 2005
prev sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
Derek, anonymous means 'no type name'
and not 'no variable name'

variable name - alias of some memory location
must always exist if you want to store something there.
So you should write something like this:

 struct S
  {
     union
     {
        int Int;
        real Atom;
     } var;
  }

And then

S s;
s.var.Int = 28;


Andrew.


"Derek Parnell" <derek psych.ward> wrote in message 
news:14ds0mvtn12la$.1nc9wu744dnbg.dlg 40tude.net...
I think the code below is supposed to compile...

 struct S
 {
   union Anon
   {
      int Int;
      real Atom;
   }
 }

 void main()
 {
    S test;
    test.Anon.Int = 2;  // line #13
 }

 But I get this message ...

  test.d(13): need 'this' to access member Int


 I need to change it to this to get it to compile ...

 struct S
 {
   union Anon
   {
      int Int;
      real Atom;
   }
   Anon A;
 }

 void main()
 {
    S test;
    test.A.Int = 2;
 }

 -- 
 Derek
 Melbourne, Australia
 1/06/2005 1:02:30 PM 

May 31 2005
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Andrew Fedoniouk wrote:
 Derek, anonymous means 'no type name'
 and not 'no variable name'
 
 variable name - alias of some memory location
 must always exist if you want to store something there.
 So you should write something like this:
 
  struct S
   {
      union
      {
         int Int;
         real Atom;
      } var;
   }

Illegal in D. Should be struct S { union Var { int Int; real Atom; } Var var; } Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 01 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message 
news:d7klmr$2090$1 digitaldaemon.com...
 Andrew Fedoniouk wrote:
 Derek, anonymous means 'no type name'
 and not 'no variable name'

 variable name - alias of some memory location
 must always exist if you want to store something there.
 So you should write something like this:

  struct S
   {
      union
      {
         int Int;
         real Atom;
      } var;
   }

Illegal in D. Should be struct S { union Var { int Int; real Atom; } Var var; } Stewart.

Thanks Stewart. It was my Cism.
Jun 01 2005