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.