This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Unnamed unions




   OK, now I'm porting some different code and it is C++ and it is using 
anonymous structs.  What's up with these?  Apparently VC++ supports 
this, but I'm not sure if it is even valid C++:


struct foo
{
     union {
         struct {
             int x, y;
         };
         int xy[2];
     };
};

int main()
{
     struct foo f;

     f.xy[0] = 1;
     f.x = 0;
}



% c++ -ansi -pedantic -Wall -c /tmp/foo.cpp
/tmp/foo.cpp:6: warning: anonymous class type not used to declare any 
objects
/tmp/foo.cpp: In function `int main()':
/tmp/foo.cpp:16: `struct foo' has no member named `x'

   So, the anonymous union is getting penetrated by the 'xy' name lookup, 
but the struct isn't getting penetrated by the 'x' lookup.

   Bug, feature?    Sadly, the code I'm porting is FULL of this sort of 
thing.  In fact it has stuff like:


union vector3 {
	struct {
		float x, y, z;
	};
	struct {
		float X, Y, Z;
	};
	struct {
		float a, b, c;
	};
	struct {
		float e1, e2, e3;
	};
	float e[3];
	float E[3];
};

   Apparently the original developers were big fans of consistency!  
<smirk>

-tim



On Friday, February 16, 2001, at 03:55  PM, Doug Landauer wrote:

>> I end up porting a lot of code that is built with VC++ and/or 
>> Metrowerks to Mac OS X using gcc.  Apparently both of these compilers 
>> support the following:
>
> My tests show gcc supporting anonymous unions as well.
> Are you maybe thinking of anonymous structs?
>
>
>> struct {
>>   union {
>> 	int a, b;
>>  };
>> } x;
>>
>> int main()
>> {
>>     x.a = 1;
>>     return 0;
>> }
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]