Thread overview
Anonymous unions
Jun 01, 2005
Derek Parnell
Jun 01, 2005
Hasan Aljudy
Jun 01, 2005
Derek Parnell
Jun 01, 2005
Chris Sauls
Jun 01, 2005
Stewart Gordon
Jun 01, 2005
Andrew Fedoniouk
Jun 01, 2005
Stewart Gordon
Jun 01, 2005
Andrew Fedoniouk
June 01, 2005
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
June 01, 2005
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.
June 01, 2005
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
June 01, 2005
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
June 01, 2005
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


June 01, 2005
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.
June 01, 2005
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;
>   }
<snip top of upside-down reply>

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.
June 01, 2005
"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;
>>   }
> <snip top of upside-down reply>
>
> Illegal in D.  Should be
>
>     struct S
>     {
>         union Var
>         {
>             int Int;
>             real Atom;
>         }
>         Var var;
>     }
>
> Stewart.

Thanks Stewart. It was my Cism.