POD (plain old data) type requirement in union

Started by
17 comments, last by hplus0603 6 years, 6 months ago

I'm sure many of you will have come across this situation before and am wondering the most elegant way of solving it:

This is an example, of wanting to create a struct/class that is an extension of a smaller version, and wanting to still be able to access the smaller version via a union. However the compiler seems to want the smaller struct to be a POD type (presumably because which constructors / destructors to call in a union is ambiguous). This is what I want, but it won't compile, complaining about Point2 being non-POD:


struct Point2
{
	Point2() {} // want this to be default
	Point2(int a, int b) {x = a; y = b;} // extra constructor for ease of use
	int x, y;
};

struct Point3
{
	union
	{
		struct
		{
			int x, y, z;
		};
		struct
		{
			Point2 xy; // access a Point3 as either a Point3 or a Point2
		};
	};
};

The only problem is it is sometimes very useful to be able to use the 2nd constructor when passing a Point2 as an argument to a function:


DrawLineTo(Point2(10, 20));

void DrawLineTo(const Point2 &pt)
{
	...
}

I am understanding there has been some changes to POD types in later c++ (I now have c++11 available), so is there any way of keeping it as a POD type while still having the secondary constructor available?

I can also see alternative methods of doing the same kind of thing (perhaps not using a union). What do you guys use?

Advertisement

Instead of giving Point2 an ease of use constructor that lets you write:
p = Point2(a, b);
These days in C++ you can not write any constructors at all and then just write:
p = Point2{a,b}
Or even just:
p = {a,b}
or
DrawLineTo({10, 20});

Fantastico! Can't believe I didn't know that worked lol! :D

Edit - Just tested it and my gcc compiler does suggest 'extended initializer lists needs c++11 flag'. So it is available. Out of interest, what would have been a good solution prior to c++11?

I also just found this article:

http://cpp11standard.blogspot.co.uk/2012/11/c11-standard-explained-1-unrestricted.html

Which does suggest you can use non-POD type in a union in c++11, however in his example there is a need to name the union so you can then give it a constructor, which would seem to make the access needlessly complex, so it doesn't seem an ideal solution...


Point3 pt;
pt.myunion.xy.x = 1;

 

Just tested the c++11 unrestricted union mentioned in the article, and it does work! Just one caveat the original code:


struct Point3
{
	union
	{
		struct
		{
			int x, y, z;
		};
		struct
		{
			Point2 xy; // access a Point3 as either a Point3 or a Point2
		};
	};
};

fails to compile with error: member 'Point2 Point3::<anonymous union>::<anonymous struct>::xy' with constructor not allowed in anonymous aggregate.

However this works:


struct Point3
{
	union
	{
		struct
		{
			int x, y, z;
		};
		Point2 xy;
	};
};

Hooray for c++11! :D
 

Don't have to put everything inside a struct:


union Point3
{
	struct
	{
		int x, y, z;
	};
	Point2 xy;
};

 

With C++17 you'll also be able to use std::variant, which is basically a type-safe union type. Not sure if C++17 is an option for you, but I thought I'd mention it.

 

That doesn't allow type punning though.

Problem is not completely solved (except by Hodgman's method)..

This still causes a problem:


struct Rect
{
	union
	{
		struct
		{
			int x, y, w, h;
		};

		struct
		{
			Point2 pos;
			Point2 size;
		};
	};
};

Giving :

error: member 'Point2 Rect::<anonymous union>::<anonymous struct>::pos' with constructor not allowed in anonymous aggregate
    Point2 pos;
 

Whereas:


struct Rect
{
	union
	{
		struct
		{
			int x, y, w, h;
		};

		struct
		{
			Point2 pos;
			Point2 size;
		} ps;
	};
};

*does* compile, but it means you would have to refer to pos and size through ps.pos and ps.size.

The only other way of doing this that springs to mind is a bit more hacky:


struct Rect
{
	Point2 &pos() {return *((Point2 *)&x);}
	Point2 &size() {return *((Point2 *)&w);}

	int x, y, w, h;
};

 

Your first example compiles fine in VS2017, maybe you can use a different compiler?

Clang will happily accept the first example with one small change:


struct Point2
{
    Point2() = default;
    Point2(int a, int b) {x = a; y = b;} // extra constructor for ease of use
    int x, y;
};
Adding the "=default;" in place of {} makes different C++11 rules apply to it and makes it a POD type.
 
To make gcc happy, you need to go one step further:
 

struct Point2
{
    Point2() = default;
    int x, y;
};
// Non-member function that works like a constructor
inline Point2 Make_Point2(int a, int b) {Point2 result; result.x = a; result.y = b; return result;}
I'm not sure if gcc is being overly strict here, or if Clang is being lenient in regards to the standard.

Ah, thanks for the info on the different compilers guys! I'm using gcc on linux, and it has to compile on android compiler too and probably iOS and windows.

Come to think of it, if the goal is simply to reduce verbosity for access, then I could use an accessor to get to the union:


struct Rect
{
	Point2 &pos {return ps.pos;}
	Point2 &size {return ps.size;}
  
	union
	{
		struct
		{
			int x, y, w, h;
		};

		struct
		{
			Point2 pos;
			Point2 size;
		} ps;
	};
};

I have found this doc on unions:

http://en.cppreference.com/w/cpp/language/union

I had no idea about the conventions of calling all the constructors / destructors as the 'lifetime' of the members changes, sounds like a nightmare lol. It almost sounds like simply casting rather than using a union is the most explicit way to do it (treat the data as binary compatible with another struct), or sticking to POD and using Hodgman's technique.

Are there any gotchas (alignment etc) to avoiding the union altogether and simply casting data members to another type, out of interest?

i.e.


struct Rect
{
	Point2 &pos() {return *((Point2 *)&x);}
	Point2 &size() {return *((Point2 *)&w);}

	int x, y, w, h;
};

 

This topic is closed to new replies.

Advertisement