Boost logo

Boost Users :

From: Kirit Sælensminde (kirit.saelensminde_at_[hidden])
Date: 2007-09-28 20:32:05


Mark Ruzon wrote:
> Suppose you want to compute 3 - x for a vector of floats:
>
> using namespace std;
> vector<float> v;
> transform(v.begin(), v.end(), v.begin(), boost::bind(minus<float>(), _1, 3.0f));
>
> This compiles fine on the MSVC 8.0 compiler. But I want to compute x - 3, and
> replacing _1 with _2 in the above expression yields a mess of errors, starting
> with:
>
> c:\program files\boost\boost_1_34_1\boost\bind.hpp(278) : error C2679: binary
> '[' : no operator found which takes a right-hand operand of type
> 'overloaded-function' (or there is no acceptable conversion)
> c:\program files\boost\boost_1_34_1\boost\bind.hpp(205): could be
> 'float boost::_bi::list1<A1>::operator [](boost::arg<I>) const'
> with
> [
> A1=float &,
> I=1
> ]
>
> Anybody have any thoughts on this one?

transform( v.begin(), v.end(), v.begin(), _1 - 3 );
transform( v.begin(), v.end(), v.begin(), 3 - _1 );

_1 represents the first argument to the final lambda (block, functor,
whatever you want to call it). _2 the second. You cannot have a second
argument without also having a first.

Assuming that minus() takes left and right as arguments one and two then
your example should already be x - 3.

Whichever expression you really want, the way to do it is to swap the
arguments to minus. You want one of these two:

boost::bind(minus<float>(), _1, 3.0f)
boost::bind(minus<float>(), 3.0f, _1)

The result of this is a unary lambda whose single argument is
represented as _1 in the expression.

K


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