Boost logo

Boost :

From: me22 (me22.ca_at_[hidden])
Date: 2006-09-20 17:10:10


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.

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.
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);

~ Scott McMurray


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