This is the mail archive of the gcc-help@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]
Other format: [Raw text]

Re: Problem with anonymous structs and unions


<mramirez@iua.upf.es> writes:

> Hi,
> 
> I am having problems to compile the following snippet of code:
> 
> struct Vector2
> {
>     union
>     {
>         struct
>         {
>             float x, y;
>         };
>         struct
>         {
>             float u, v;
>         };
>     };
> };
> 
> int main( int argc, char** argv )
> {
> 
>     Vector2 vertex;
> 
>     vertex.x = 100.0f;
>     vertex.y = 50.0f;
> 
>     Vector2 texCoord;
> 
>     texCoord.u = 0.75f;
>     texCoord.v = 0.025f;
> 
>     return 0;
> }
> 
> g++-2.95 complains in the following manner:
> 
> mramirez@mtg100:~/anonymous$ g++ -o test test.cxx
> test.cxx:11: anonymous class type not used to declare any objects
> test.cxx:15: anonymous class type not used to declare any objects
> 
> Since this works fine under other compilers ( don't ask which ones
> ), I am suspecting that that union-structtuple is violating either
> the standard or the gcc "way of doing things", isn't it?

Are those other compilers Windows compilers, by chance? They are
forced to be bug-compatible with MSVC++.

Anonymous structs must declare an object, as the error message says.

> For the case above mentioned there's a simple workaround:
> 
> struct Vector2
> {
>     union
>     {
>         float x;
>         float u;
>     };
> 
>     union
>     {
>         float y;
>         float v;
>     };
> };
> 
> So I can port most of these expressive vector types, except when
> expressivity is further augmentedby providing a union with an array,
> for instance. I could relay on returning a pointer to the first
> memberdeclared, but that's likely to provide interesting bugs when
> combined with "exotic" alignments.  Any ideas?

How about naming the structs?

-- 
Oscar



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