Boost logo

Boost :

From: Olivier Grant (olivier.grant_at_[hidden])
Date: 2006-09-20 18:28:25

On 9/20/06, me22 <me22.ca_at_[hidden]> wrote:
> On 9/20/06, Olivier Grant <olivier.grant_at_[hidden]> wrote:
> > You could just wrap vector_add in an operator+, but the original
> intension
> > was to provide not only a generic way of handling any type of binary
> > operation between vectors and/or scalars, but also to have it compile to
> the
> > most efficient code possible. I also wanted to allow the user to know
> > exactly what he is doing when performing function calls :
> >
> > res = v1 * scalar + v2;
> >
> > I think the following syntax really makes you sure you control what is
> > happening :
> >
> > vector_mul(v1, scalar, v1);
> > vector_add(v1, v2, res);
> >
> > In highly sensitive software that needs to perform very fast, I think
> > developers would prefer this kind of syntax. The other advantage I see
> is to
> > easily allow developers to use this function as a default implementation
> > eventually replacing it with a architecture specific version if
> > necessary/possible.
> >
> Personally, I'd much prefer the infix version. And of course, your 2
> snippets do rather different things.
> One advantage of expression templates is that you can write res = v1 *
> scalar + v2; and still have it be evaluated the efficient way without
> the temporaries.

I agree, i'm not saying you can't generate as efficient code - after all,
operators are just like functions pretty much - but just that it sometimes
gives a better sense of control over what you are doing. But this is just a

As for your way, you can do the same thing without the confusion of
> ternary "add" simple by overloading @= :
> res = v1;
> res *= scalar;
> res += v2;
> No temporaries and possibly even faster than your ternary version.

I've actually compared the resulting assembly code generated by gcc between
the two code snipplets and it turns out they match. You could anyway just
wrap the functions in their equilalent operator overloads.

And a whole lot easier to read -- I would be VERY confused by which is
> in and which is out in vector_add(v1, v2, v3);


Sorry, just thought it would be funny. I agree that operators make for a
much more readable and intuitive syntax, but I think that with good
documentation, as comments and in the choise of parameter names, and
consistency, you should be able to use these functions without

~ Scott McMurray
> _______________________________________________
> Unsubscribe & other changes:

Boost list run by bdawes at, gregod at, cpdaniel at, john at