Recently I've been checking out the source-code of GLM (OpenGL Mathematics Library) And I noticed that in their vec4 class (starting on line 45) they do something interesting. Let's pretend I have some vec4 named A. In order to get the behavior that A.x, A.r, and A.s all refer to the same value (the first element of the vec4), and A.y, A.g, and A.t all refer to the second element and so on and so forth, GLM takes two approaches.
First, if anonymous structs are enabled (which I know is iffy/will depend on the compiler if supported), GLM will declare the class with a grouping of anonymous structs (let's call this Approach #1):
union {
struct { T x, y, z, w; };
struct { T r, g, b, a; };
struct { T s, t, p, q; };
};
However, if anonymous structs are not enabled, they will declare the class like so with anonymous unions (let's call this Approach #2):
union { T x, r, s; };
union { T y, g, t; };
union { T z, b, p; };
union { T w, a, q; };
Now, IIRC Approach #1 is technically undefined behavior in C++, but is supported by many compilers. But Approach #2 should always work. But since it seems GLM prefers the more controversial Approach #1 with Anonymous Structs, I'm wondering why. Why not just always use the safer Approach #2? Are there any performance differences that arise either in compilation, run-time, or usage that differ between these two approaches?