Boost logo

Boost :

Subject: Re: [boost] [functional] New functor adaptors
From: Jonathan Wakely (jwakely.boost_at_[hidden])
Date: 2013-09-12 08:52:46


On 12 September 2013 13:03, Tigran Hayrapetyan wrote:
> Hello,
>
> Suppose we want to transform sequence of integers by doubling and adding 5
> to each value (x = 2*x + 5 for all x in sequence). If using std::transform,
> then we need a functor which will for given "x" return "2*x + 5". My
> question is - is it possible to do it in C++98 style (without lambda
> expressions), and without writing additional functor class?

    std::transform(begin, end, out,
            boost::bind( std::plus<int>(), 5,
                boost::bind( std::multiplies<int>(), 2, _1 ) ) );

> As I understand, it's not. For doing that we need something like nesting
> functors into each other. Here we have "std::bind2nd(
> std::multiplies<int>(), 2 )" functor, and "std::bind2nd( std::plus<int>(),
> 5 )" functor, which must be nested to provide necessary functionality.

So don't use std::bind2nd, use boost::bind which is superior in every way.


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