我需要将绑定函数传递给另一个函数,但是却收到错误消息,没有可用的转换-
cannot convert argument 2 from 'std::_Bind<true,std::string,std::string (__cdecl *const )(std::string,std::string),std::string &,std::_Ph<2> &>' to 'std::function<std::string (std::string)> &'
功能:
std::string keyFormatter(std::string sKeyFormat, std::string skey)
{
boost::replace_all(sKeyFormat, "$ID$", skey);
return sKeyFormat;
}
用法就像-
auto fun = std::bind(&keyFormatter, sKeyFormat, std::placeholders::_2);
client(sTopic, fun);
客户端功能看起来像-
void client(std::function<std::string(std::string)> keyConverter)
{
// do something.
}
您使用的是错误的placeholders
,您需要_1
:
auto fun = std::bind(&keyFormatter, sKeyFormat, std::placeholders::_1);
此处的占位符编号不匹配args的位置,而是选择将哪个参数发送到哪个位置的原始函数:
void f (int, int);
auto f1 = std::bind(&f, 1, std::placeholders::_1);
f1(2); // call f(1, 2);
auto f2 = std::bind(&f, std::placeholders::_2, std::placeholders::_1);
f2(3, 4); // call f(4, 3);
auto f3 = std::bind(&f, std::placeholders::_2, 4);
f3(2, 5); // call f(5, 4);
请参阅std::bind
,尤其是最后的示例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句