Boost logo

Boost Users :

From: boost_at_[hidden]
Date: 2005-09-07 14:05:25


>I would like to do a compile-time mapping of an array accessor to a list of
> scalars as in the following class:

Hi Steven,

This is something I found floating on the net some time ago, perhaps it will
give you some ideas:

template <typename T>
class Vector4
{
   typedef T Vector4<T>::* const vec[4];
   static const vec v;

public:
   T x, y, z, w;

   Vector4()
    : x(0),
      y(0),
      z(0),
      w(0)
   {
   }

   Vector4(T _x, T _y, T _z, T _w)
    : x(_x),
      y(_y),
      z(_z),
      w(_w)
   {
   }

   const T& operator[](size_t i) const
   {
      return this->*v[i];
   }

   T& operator[](size_t 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>::w };

template <typename T>
class Matrix4
{
   typedef Vector4<T> Matrix4::* const mat[4];
   static const mat a;

public:
   Vector4<T> i, j, k, l;

   Matrix4()
    : i(),
      j(),
      k(),
      l()
   {
   }

   Matrix4(const Vector4<T>& _i, const Vector4<T>& _j, const Vector4<T>& _k,
const Vector4<T>& _l)
    : i(_i),
      j(_j),
      k(_k),
      l(_l)
   {
   }

   const Vector4<T>& operator[](size_t i) const
   {
      return this->*a[i];
   }

   Vector4<T>& operator[](size_t i)
   {
      return this->*a[i];
   }
};

template <typename T>
const typename Matrix4<T>::mat Matrix4<T>::a = { &Matrix4<T>::i,
&Matrix4<T>::j, &Matrix4<T>::k, &Matrix4<T>::l };

Cheers,
Anders Dalvander


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net