# C++ POD (plain old data) type requirement in union

## Recommended Posts

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?

Edited by lawnjelly

##### Share on other sites

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});

##### Share on other sites

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

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?

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;

Edited by lawnjelly
tested

##### Share on other sites

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!

##### Share on other sites

Don't have to put everything inside a struct:

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

##### Share on other sites

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.

Edited by rnlf_in_space

##### Share on other sites

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;
};

##### Share on other sites

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

##### Share on other sites

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.

##### Share on other sites

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:

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;
};

##### Share on other sites
3 hours ago, lawnjelly said:

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

Yes, even though there probably wouldn't be any alignment issues in this case*, there's also strict aliasing you have to keep in mind. Aliasing is when you have to two pointers pointing to the same location, with strict aliasing the compiler usually assumes that two pointers of different types never point to overlapping locations**. That's great for performance as the compiler is free to reorder access to those locations because it's assumed they don't overlap, but if you break the strict aliasing rule you can get undefined behaviour.

** There are exceptions to this rule, like char* always aliases other types, here's a good intro to strict aliasing. Also, not an expert on this.

##### Share on other sites
20 hours ago, Mussi said:

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

Use W4 as warning level, then you'll notice that anonymous structs are non-standard C++.

Standard C++ only supports anonymous unions due to C legacy.

Edited by matt77hias

##### Share on other sites
1 hour ago, matt77hias said:

Use W4 as warning level, then you'll notice that anonymous structs are non-standard C++.

Getting no warnings in VS2017, even with /Wall :-o. I do remember seeing warnings about anonymous structs in the past though, not sure what changed.

##### Share on other sites
15 minutes ago, Mussi said:

Getting no warnings in VS2017, even with /Wall :-o. I do remember seeing warnings about anonymous structs in the past though, not sure what changed.

I specifically disabled them in VS2017: code 4201.

##### Share on other sites

Come to think of it, does this compile without any errors?

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

No aggregates of anonymous structs, so might work?

##### Share on other sites
16 hours ago, lawnjelly said:

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

Yeah if any of the structures are using custom alignment (e.g. 16 byte alignment so that they will generate nice SSE instructions), then dodgy casting that causes the alignment to be incorrect may cause crashes.

In your case though, you're only using plaint ints which should all have the same alignment (probably 4B on a common compiler).

##### Share on other sites
12 hours ago, Mussi said:

Come to think of it, does this compile without any errors?


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

No aggregates of anonymous structs, so might work?

It does!  Ha good bit of lateral thinking lol. Just shows there is usually a way around these problems, but it always seems a bit of shoehorning a solution.

As matt77hias says, the anonymous struct doesn't seem to be technically allowed in c++, but lots of people use them.. and hence the different results on different compilers I guess.

Alignment sounds like it isn't likely to be a problem with casting, but aliasing bugs is a good call it sounds something to watch for.

##### Share on other sites

You cannot put things with constructors into unions, for the reason you state in the very top of your post: constructors/destructors become ambiguous. Which destructor, if any, should the compiler call when the union goes out of scope?

The work-around involves inheritance and constructors:

class Point2;

struct Point2Base {
float x;
float y;
Point2 &asPoint() { return *(Point2*)this; }
};

class Point2 : public Point2Base {
public:
// WARNING: Not explicit!
Point2(Point2Base const &c) : Point2Base(c) {}
void myFunction();
};

union MyUnion {
struct {
float x, y, z;
} a;
struct {
Point2Base p;
} b;
};

// OBSERVE: Value, not reference
void function_taking_point2(Point2 p2) {
}

int main() {
MyUnion u;
function_taking_point2(u.b.p);
return 0;
}

VERY IMPORTANT though: If you pass u.b.p by reference, a temporary will be created, and you'll see the reference to the temporary in the called function, which is not why you want. This is why single-argument constructors should generally be made "explicit." That will break the convenience of passing values to functions, and you'll have to use asPoint() instead.

Here's a safer, slightly less convenient version:

class Point2;

struct Point2Base {
float x;
float y;
Point2 &asPoint() { return *(Point2*)this; }
};

class Point2 : public Point2Base {
public:
// GOOD: uses explicit constructor
explicit Point2(Point2Base const &c) : Point2Base(c) {}
void myFunction();
};

union MyUnion {
struct {
float x, y, z;
} a;
struct {
Point2Base p;
} b;
};

// Uses reference
void function_taking_point2(Point2 const &p2) {
}

int main() {
MyUnion u;
// Must use cast function
function_taking_point2(u.b.p.asPoint());
return 0;
}

Finally, note that the member function "asPoint()" doesn't disqualify Point2Base from being considered POD.

Constructors, destructors, and virtual member functions will disqualify a compound, but not plain member functions or static member functions.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628312
• Total Posts
2981998
• ### Similar Content

• By HD86
As far as I know, the size of XMMATRIX must be 64 bytes, which is way too big to be returned by a function. However, DirectXMath functions do return this struct. I suppose this has something to do with the SIMD optimization. Should I return this huge struct from my own functions or should I pass it by a reference or pointer?
This question will look silly to you if you know how SIMD works, but I don't.

• Hey, Im using directx allocate hierarchy from dx9 to use a skinned mesh system.
one mesh will be only the skeleton with all animations others meshes will be armor, head etc, already skinned with skeleton above. No animation, idle position with skin, thats all I want to use the animation from skeleton to other meshes, so this way I can customize character with different head, armor etc. What I was thinking its copy bone matrices from skeleton mesh to others meshes, but Im a bit confused yet what way I can do this.

Thanks.

• Does buffer number matter in ID3D11DeviceContext::PSSetConstantBuffers()? I added 5 or six constant buffers to my framework, and later realized I had set the buffer number parameter to either 0 or 1 in all of them - but they still all worked! Curious why that is, and should they be set up to correspond to the number of constant buffers?
Similarly, inside the buffer structs used to pass info into the hlsl shader, I added padding inside the c++ struct to make a struct containing a float3 be 16 bytes, but in the declaration of the same struct inside the hlsl shader file, it was missing the padding value - and it still worked! Do they need to be consistent or not? Thanks.
struct CameraBufferType
{
XMFLOAT3 cameraPosition;
};
• By esenthel
Just finished making latest WebGL demo for my game engine:
http://esenthel.com/?id=live_demo
Let me know what you think,
as of now only Chrome and Firefox can run it.
Edge, Safari, Opera have some unresolved bugs at the moment.
• By reders
Hi, everyone!
I "finished" building my first game. Obviously Pong.
It's written in C++ on Visual Studio with SFML.
Pong.cpp
What do you think? What should I consider doing to improve the code?
Thank you very much.

EDIT: added some screenshot and .zip file of the playable game

Pong.zip

• 9
• 9
• 13
• 11
• 13