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]

Anonymous structures with constructors.


Hello all!

I'm having a little bit of difficulties porting some code from windows to
Linux. The windows code currently uses the Microsoft compiler, version 7.1
and the following code compiles just fine there. As I understand it's a
specific MS-extension that allows structures with constructors/destructors
inside unions. Are there any possibility that I could compile this code
unmodified with any gcc-version?

As I understand when MSVC generates code for the Transform class, it doesn't
add calls to any Vector constructors (which makes sense, but can be a
potential source of a bullet through a foot). The vector constructors are
necessary for easy conversions from other types, e.g. tuples of floats and
other hardware specific types.

Regards,
Jim Tilander

--- anonymousstruct.cpp ---
#include <cstdio>

class Vector
{
public:
	Vector() {}
	Vector( float x_, float y_, float z_ ) {}
public:
	union
	{
		struct
		{
			float x,y,z;
		};

		float e[4];
	};
};


class Transform
{
public:
	Transform() {}
	~Transform() {}

public:
	union
	{
		struct
		{
			Vector xAxis;
			Vector yAxis;
			Vector zAxis;
			Vector translation;
		};

		struct
		{
			Vector c[4];
		};

		float e[16];
	};
};


int main( int argc, char* argv[] )
{
	{
		printf( "Marker\n" );
		Transform t;
		printf( "%f\n", t.xAxis.x );
	}

	return 0;
}

--- cut here ---

--- compiler output below ---

Balrog:~/temp> /usr/local/gcc332/bin/g++ -v
Reading specs from
/usr/local/gcc332/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/specs
Configured with: ./configure --prefix=/usr/local/gcc332
Thread model: posix
gcc version 3.3.2
Balrog:~/temp> /usr/local/gcc332/bin/g++ -c -Wall anonymousstruct.cpp
anonymousstruct.cpp:33: error: member Vector Transform::<anonymous
   union>::<anonymous struct>::xAxis' with constructor not allowed in
anonymous
   aggregate
anonymousstruct.cpp:34: error: member Vector Transform::<anonymous
   union>::<anonymous struct>::yAxis' with constructor not allowed in
anonymous
   aggregate
anonymousstruct.cpp:35: error: member Vector Transform::<anonymous
   union>::<anonymous struct>::zAxis' with constructor not allowed in
anonymous
   aggregate
anonymousstruct.cpp:36: error: member Vector Transform::<anonymous
   union>::<anonymous struct>::translation' with constructor not allowed in
   anonymous aggregate
anonymousstruct.cpp:33: error: member `Vector Transform::<anonymous
   union>::<anonymous struct>::xAxis' with constructor not allowed in union
anonymousstruct.cpp:34: error: member `Vector Transform::<anonymous
   union>::<anonymous struct>::yAxis' with constructor not allowed in union
anonymousstruct.cpp:35: error: member `Vector Transform::<anonymous
   union>::<anonymous struct>::zAxis' with constructor not allowed in union
anonymousstruct.cpp:36: error: member `Vector Transform::<anonymous
   union>::<anonymous struct>::translation' with constructor not allowed in
   union
anonymousstruct.cpp:41: error: member `Vector Transform::<anonymous
   union>::<anonymous struct>::c[4]' with constructor not allowed in union


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