
Miles Bader wrote:
"Phil Endecott" <spam_from_boost_dev@chezphil.org> writes:
http://www.gamedev.net/community/forums/topic.asp?topic_id=261920 Yes, a very good page showing several solutions:
1. A vector of member pointers (may have some overhead). 2. Anonymous structs and unions (may be non-standard). 3. x, y and z implemented as references (makes object bigger). 4. Relying on (&x)[1] pointing to y.
Regarding (2), note that using a union can cause severe performance degradation with some compilers (recent versions of gcc, at least). I guess that the compiler makes pessimistic assumptions about aliasing when it sees a union.
As an example: I have some code that offers both ".x/.y/.z" and array-style access to the members of 3d vectors.
The code originally only used ".x/.y/.z" fields. When I first added the "array-style" feature, I used a construct like:
union { struct { float x, y, z; }; float els[3]; }; float &operator[] (unsigned i) { return els[i]; } const float &operator[] (unsigned i) const { return els[i]; }
The resulting object code was _much_ worse than the original.
Not to mention that you can't put non-POD types into a union which makes this option a non-starter for generic code. - Michael Marcin