Boost logo

Boost :

From: me22 (me22.ca_at_[hidden])
Date: 2006-09-20 14:11:27


On 9/20/06, Olivier Grant <olivier.grant_at_[hidden]> wrote:
> Right now I have three simple vector classes - ranging from vector2d to
> vector4d - that are not templetized since I like the idea of being able to
> access coordinates directly by their name
>
> vector3d vec;
> vec.x += 10.0f;
>
Thanks to SFINAE you can do that for the general case, so long as you
don't mind
    vec.x() += 10;
Which I don't think is a great hardship.

In a math::vector<T,N>, for example, you'd have code like this:
boost::enable_if_c< N >= 1, T& > x() { return v[0]; }
boost::enable_if_c< N >= 2, T& > y() { return v[1]; }
boost::enable_if_c< N >= 3, T& > z() { return v[2]; }
boost::enable_if_c< N >= 4, T& > w() { return v[3]; }

I've written a fully-templated math::vector class that you can find at
http://gpwiki.org/index.php/C_plus_plus:Tutorials:TemplateVector (not
using boost though, so it does a bad form of STATIC_ASSERT instead of
the enable_if ).

That being said, why can't you use uBLAS (
http://boost.org/libs/numeric/ublas/doc/vector.htm ) or some other
premade library? Other people have already written heavily-optimized
ones, using expression templates and such to keep the nice syntax
without adding inefficiencies. (No need for a 3-argument add
function, for example.)

~ Scott McMurray


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk