This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Problem with anonymous structs and unions
- From: <mramirez at iua dot upf dot es>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Wed, 13 Nov 2002 10:36:25 +0100 (CET)
- Subject: Problem with anonymous structs and unions
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?
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?
Miguel Ramírez