Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 959
Zepee Avatar asked Feb 05 '26 21:02

Zepee


1 Answers

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

like image 94
songyuanyao Avatar answered Feb 08 '26 09:02

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!