Question

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.

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top