# Random Union Question

## Recommended Posts

I'm attempting to make a vector and matrix classes as similar to GLSL as possible and have had very little experience with what would be considered proper programming methods so I was curious what people thought of using unions like such:
typedef struct _vec3 {
union {
float xyz[3];
float rgb[3];
float stp[3];

struct {
union {
float xy[2];
float rg[2];
float st[2];

struct {
union {
float x;
float r;
float s;
};
union {
float y;
float g;
float t;
};
};
};
union {
float z;
float b;
float p;
};
};

struct {
union {
float x;
float r;
float s;
};
union {
float yz[2];
float ga[2];
float tp[2];

struct {
union {
float y;
float g;
float t;
};
union {
float z;
float b;
float p;
};
};
};
};
};
} vec3;


I know it looks hideous (as are all great sleep deprived ideas), but I'm more interested in compatibility and efficiency. I actually found it interesting that it compiles (using MS VS2008), since some variable names are the same. I tested it with a very simple case and it gave me correct results. I also made a vec2 struct similar to this one and used it instead of the float[2]s in this vec3 and it worked as well. Is it guaranteed that the results will be correct?

##### Share on other sites
Using it like GLSL vectors would probably result in undefined behaviour as far as the standard is concerned.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627652
• Total Posts
2978424

• 10
• 12
• 22
• 13
• 33