3

My question is pretty straightforward: can I do something like this?

Say class foo contains the following member function:

foo foo::DoSomething(input_type1 input1, input_type2 input2)
{
    ... // Adjust private datamembers
    return *this;
}

Using foo:

std::vector<foo> foovec;
input_type1 in1;
input_type2 in2;
...
std::transform(foovec.begin(), foovec.end(), foovec.begin(), std::mem_fun_ref(boost::bind(&foo::DoSomething, in1, in2)));

So is this possible? The issue is pretty much whether boost::bind() has an effect on the member/nonmember nature of the function it works on. I reckon I can't go about it the other way around like this:

std::transform(foovec.begin(), foovec.end(), foovec.begin(), boost::bind(std::mem_fun_ref(&foo::DoSomething), _1, in1, in2)));

because std::mem_fun_ref() takes a unary or nullary function and DoSomething() is binary.

Wouter
  • 161
  • 1
  • 6
  • 1
    If you're going to use Boost Bind, wouldn't you want to use [Boost.Function](http://www.boost.org/doc/libs/1_49_0/doc/html/function.html) with it? – Jerry Coffin Apr 18 '12 at 00:35
  • 2
    What version of Boost are you using? `boost::bind(&foo::DoSomething, _1, in1, in2)` should work out of the box. [As is documented](http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#with_member_pointers). Same goes for `boost::phoenix::bind` (since Boost.Phoenix is supposed to be the superior alternative to Boost.Lambda which is supposed to be the superior alternative to Boost.Bind) and so on. – Luc Danton Apr 18 '12 at 00:49

1 Answers1

3

You don't need std::mem_fun_ref, just use:

std::transform(foovec.begin(),
               foovec.end(),
               foovec.begin(),
               boost::bind(&foo::DoSomething, _1, in1, in2));

or you could replace boost::bind with

std::bind(&foo::DoSomething, std::placeholders::_1, in1, in2)
Fraser
  • 74,704
  • 20
  • 238
  • 215