Search code examples
c++c++11stdstdbind

Using std::bind to bind parameters and object instance separately


Is it possible to bind arguments to a member function call, and later bind the target object separately?

What I was trying to achieve is a helper function which receives a function with arbitrary arguments as an argument, and then executes it on all items in a collection.

void SomeType::Work(UINT a, UINT b, UINT c)
{
    //do something
}

template<typename Function, class Container>
void Execute(const Function& fn, const Container& collection)
{
    for(auto& item : collection)
    {
        auto bound = std::bind(fn, &item);
        bound();
    }
}

void Test()
{
    //eg. container = vector of SomeType
    auto fn = std::bind(&Sometype::Work, 10, 20, 30);
    Execute(fn, container);
}

This was failing with some error within functional:

error C2064: term does not evaluate to a function taking 3 arguments

Eventually pointing at:

see reference to function template instantiation 'void Execute<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall SomeType::* )(UINT,UINT,UINT),void,SomeType,UINT,UINT,UINT>,UINT &,UINT &,UINT &>>(const Function &)' being compiled
with
[
    Function=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall SomeType::* )(UINT,UINT,UINT),void,SomeType,UINT,UINT,UINT>,UINT &,UINT &,UINT &>
]

I worked around it by just passing in a lambda that captures the desired values and executes the desired function on an object passed into it. I'm still left wondering if binding functions this way has any legs and I was just going about it wrong, or if it's just not doable.


Solution

  • You should leave a placeholder for the target object which will be bound at the 2nd time binding, when bind the member function Sometype::Work().

    Try this:

    using namespace std::placeholders;
    auto fn = std::bind(&Sometype::Work, _1, 10, 20, 30);
                                         ~~
    

    LIVE