This one’s going to be quick; it’s a little trick I came up with [not that I’m under the impression I’m the first or only one to come up with it].

The trick is to do with defining templated vectors, I’m not actually using it at the moment since I’ve taken to a different route but it struck me as nice enough to post up.

Basically what I had was a BaseData struct which defined a union and was used as both a Matrix base and a Vector base; here’s the code:

`template`

< const unsigned R

, const unsigned C

, typename T

>

class BaseData

{

protected:

union{

m[R*C];

mm[R][C];

};

};

The BaseData iteslf is a bit more complicated obviously, having overloaded operators and constructors but for the sake of clarity.

The first thing I wanted to add to it was the ability to discern between RowMajor and ColMajor matrices [a valid distinction as also noticed recently by a friend of mine on his own blog]

So I’ve quickly whipped up a little enum;

`enum MajorOrder`

{

ROW_MAJOR=0,

COL_MAJOR=1

};

Take a quick note of the values assigned because that’s going to come in handy in a minute.

Now simply add that to the template arguments:

`template`

< const unsigned R

, const unsigned C

, typename T

, MajorOrder OrderType

>

class BaseData

{

protected:

union{

m[R*C];

mm[R][C];

};

};

Matrix, taken care of. But I’m also using it for vectors and those of you with a sharp eye [and you know who you are] probably already noticed a bit of a problem because this setup lets me define a vector using R=3 and C=7 and that’s not a vector at all, is it?

The question, as it turned out, wasn’t too hard to solve, the Vector base class looks like this:

`template`

< const unsigned D

, typename T

, MajorOrder OrderType

>

class VectorBase : public BaseData<hmm, what goes in here>{};

Whoops, another problem, it’s easy to say a vector can only be defined using a dimension value, but how will that translate into rows and columns? and how to get the compiler [template remember] to figure it all out?

Next on the agenda was to define the relationship between the rows, cols and order type in mathematical terms [remember that order can only be 1 or 0]. Eventually I came up with:

`rows=(dimension*order)+(!order);`

cols=dimension/rows;

And there you go, compile time decision on how many rows/cols the vector has while allowing me to decide whether I’m using row vectors or column vectors.

`template`

< const unsigned D

, typename T

, MajorOrder OrderType

>

class VectorBase

: public BaseData

< (D*OrderType)+!OrderType

, D/((D*OrderType)+!OrderType)

, T>{};