From: Theodore Papadopoulo (Theodore.Papadopoulo_at_[hidden])
Date: 2006-09-21 12:18:10


On Thu, 2006-09-21 at 08:54 -0400, Michael Fawcett wrote:

>
> You can get around this using something like this:
>
> template< typename T >
> struct Vector4 {
>
> typedef size_t size_type;
>
> private:
>
> typedef T Vector4<T>::* const vec[4];
>
> static const vec v;
>
> public:
>
> T x, y, z, w;
>
> Vector4(T _x = 0, T _y = 0, T _z = 0, T _w = 0):
> x(_x), y(_y), z(_z), w(_w) {}
>
> const T& operator[](size_type i) const {
> return this->*v[i];
> }
>
> T& operator[](size_type i) {
> return this->*v[i];
> }
>
> };
>
> template< typename T >
> const typename Vector4<T>::vec Vector4<T>::v = { &Vector4<T>::x,
> &Vector4<T>::y, &Vector4<T>::z, &Vector4<T>::z };
>
> Here's the original discussion from gamedev.net
> http://www.gamedev.net/community/forums/topic.asp?topic_id=261920
>
> This wasn't written by me, I just remembered it from there and thought
> it might be interesting to you.

Interesting ! But I would certainly prefer the straightforward array
implementation and an enum {X,Y,Z,Z} that allows the notation

V[X] or V(X) (depending on your preference). The only fear I may have
with your proposal is that by having complicated accesses, the compiler
might not be able to do some optimizations.

        Theo.