使用std::bind分别绑定参数和对象实例
Using std::bind to bind parameters and object instance separately
是否可以将参数绑定到成员函数调用,然后单独绑定目标对象?
我试图实现的是一个辅助函数,它接收一个带有任意参数的函数作为参数,然后对集合中的所有项目执行它。
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);
}
由于功能中的一些错误而失败:
error C2064: term does not evaluate to a function taking 3 arguments
最终指向:
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 &>
]
我通过传入一个 lambda 来解决它,该 lambda 捕获所需的值并对传递给它的对象执行所需的函数。我仍然想知道以这种方式绑定函数是否有任何意义,我只是做错了,或者它是否不可行。
在绑定成员函数时,第二次绑定时要为绑定的目标对象留一个占位符Sometype::Work()
。
试试这个:
using namespace std::placeholders;
auto fn = std::bind(&Sometype::Work, _1, 10, 20, 30);
~~
听起来您只是想将函数(带有特定参数)应用于集合。
我看不出这与使用具有特定功能的 std::for_each
有何不同。
void Test()
{
//eg. container = vector of SomeType
auto fn1 = [/*whatever*/](const container_value_type& obj) {
Sometype::Work(10, 20, 30, obj);
}
auto fn2 = [/*whatever*/](const container_value_type& obj) {
Sometype::Work(40, 50, 60, obj);
}
std::for_each(container.begin(), container.end(), fn1);
std::for_each(container.begin(), container.end(), fn2);
}
是否可以将参数绑定到成员函数调用,然后单独绑定目标对象?
我试图实现的是一个辅助函数,它接收一个带有任意参数的函数作为参数,然后对集合中的所有项目执行它。
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);
}
由于功能中的一些错误而失败:
error C2064: term does not evaluate to a function taking 3 arguments
最终指向:
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 &>
]
我通过传入一个 lambda 来解决它,该 lambda 捕获所需的值并对传递给它的对象执行所需的函数。我仍然想知道以这种方式绑定函数是否有任何意义,我只是做错了,或者它是否不可行。
在绑定成员函数时,第二次绑定时要为绑定的目标对象留一个占位符Sometype::Work()
。
试试这个:
using namespace std::placeholders;
auto fn = std::bind(&Sometype::Work, _1, 10, 20, 30);
~~
听起来您只是想将函数(带有特定参数)应用于集合。
我看不出这与使用具有特定功能的 std::for_each
有何不同。
void Test()
{
//eg. container = vector of SomeType
auto fn1 = [/*whatever*/](const container_value_type& obj) {
Sometype::Work(10, 20, 30, obj);
}
auto fn2 = [/*whatever*/](const container_value_type& obj) {
Sometype::Work(40, 50, 60, obj);
}
std::for_each(container.begin(), container.end(), fn1);
std::for_each(container.begin(), container.end(), fn2);
}