如何使用函数指针映射正确使用 boost::bind
How to use boost::bind correctly with a map of function pointers
我正在尝试实现类似的目标,但遇到与提升绑定相关的错误
#include <map>
#include "boost/assign.hpp"
#include <boost/foreach.hpp>
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
struct myStruct_t
{
int getInt () {return 1;};
};
int foo( myStruct_t* m, std::string str)
{
if(str == "A")
{
return m->getInt();
}
else if (str == "B")
{
return 2;
}
else
{
return 3;
}
}
typedef std::map<std::string, boost::function<int(myStruct_t*, std::string)> > Map_t;
Map_t myMap = boost::assign::map_list_of ("Jake", boost::bind((&foo, _1), "A")
("Ken", boost::bind((&foo, _1), "B")
("Pete", boost::bind((&foo, _1), "C");
int main ()
{
std::vector<std::string> myVal;
myVal.push_back("Jake");
myVal.push_back("Ken");
myVal.push_back("Pete");
myStruct_t myStruct;
BOOST_FOREACH( const std::string& aStr, myVal)
{
Map_t::const_iterator iter = myMap.find(aStr);
if(iter != myMap.end())
{
int myInt = (iter->second)(myStruct);
}
}
return 0;
}
我得到的错误是
In file included from /usr/local/boost-1.60.0/include/boost/bind.hpp:22:0,
from prog.cc:6:
/usr/local/boost-1.60.0/include/boost/bind/bind.hpp: In instantiation of 'boost::_bi::result_traits<boost::_bi::unspecified, boost::arg<1> >':
/usr/local/boost-1.60.0/include/boost/bind/bind.hpp:1212:48: instantiated from 'boost::_bi::bind_t<boost::_bi::unspecified, boost::arg<1>, boost::_bi::list1<boost::_bi::value<const char*> > >'
prog.cc:32:81: instantiated from here
/usr/local/boost-1.60.0/include/boost/bind/bind.hpp:75:37: error: no type named 'result_type' in 'struct boost::arg<1>'
prog.cc:34:82: error: expected ')' before ';' token
prog.cc: In function 'int main()':
prog.cc:50:48: error: no match for call to '(const boost::function<int(myStruct_t*, std::basic_string<char>)>) (myStruct_t&)'
/usr/local/boost-1.60.0/include/boost/function/function_template.hpp:765:17: note: candidate is: result_type boost::function2<R, T1, T2>::operator()(T0, T1) const [with R = int, T0 = myStruct_t*, T1 = std::basic_string<char>, result_type = int]
看来我对boost::bind的使用方式感到困惑。有人可以帮我正确地做吗?非常感谢。
调用点预期的函数签名是 int(myStruct*)
因为这一行(我添加了 & 以消除逻辑错误):
int myInt = (iter->second)(&myStruct);
在这种情况下,地图的声明应该是:
typedef std::map<std::string, boost::function<int(myStruct_t*)> > Map_t;
Map_t myMap = boost::assign::map_list_of
("Jake", boost::bind(&foo, boost::placeholders::_1, std::string("A")))
("Ken", boost::bind(&foo, boost::placeholders::_1, std::string("B")))
("Pete", boost::bind(&foo, boost::placeholders::_1, std::string("C")));
然后就可以开始了。
解释:
boost[std]::bind returns 一个函数对象,旨在仅接受在 bind
表达式中作为占位符提及的参数。
这意味着最终调用的函数通常比 bind
返回的函数对象有更多的参数
因此在您的情况下,foo
具有以下签名:
int foo( myStruct_t* m, std::string str)
即接受两个参数和 returns 一个整数。
但是在调用 bind
时:
boost::bind(&foo, boost::placeholders::_1, std::string("A"))
我们所说的是“捕获函数 foo
和第二个参数(字符串)。Return 我是一个函数对象,需要一个参数 (_1
) 和将该参数作为第一个参数转发给 foo
,同时将绑定的字符串作为第二个参数传递。
如此给出:
auto f = boost::bind(&foo, boost::placeholders::_1, std::string("A"));
f
具有签名 int f(MyStruct*)
并且当用
调用时
auto i = f(&mystruct);
相当于调用:
auto i = foo(&mystruct, std::string("A"));
我正在尝试实现类似的目标,但遇到与提升绑定相关的错误
#include <map>
#include "boost/assign.hpp"
#include <boost/foreach.hpp>
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
struct myStruct_t
{
int getInt () {return 1;};
};
int foo( myStruct_t* m, std::string str)
{
if(str == "A")
{
return m->getInt();
}
else if (str == "B")
{
return 2;
}
else
{
return 3;
}
}
typedef std::map<std::string, boost::function<int(myStruct_t*, std::string)> > Map_t;
Map_t myMap = boost::assign::map_list_of ("Jake", boost::bind((&foo, _1), "A")
("Ken", boost::bind((&foo, _1), "B")
("Pete", boost::bind((&foo, _1), "C");
int main ()
{
std::vector<std::string> myVal;
myVal.push_back("Jake");
myVal.push_back("Ken");
myVal.push_back("Pete");
myStruct_t myStruct;
BOOST_FOREACH( const std::string& aStr, myVal)
{
Map_t::const_iterator iter = myMap.find(aStr);
if(iter != myMap.end())
{
int myInt = (iter->second)(myStruct);
}
}
return 0;
}
我得到的错误是
In file included from /usr/local/boost-1.60.0/include/boost/bind.hpp:22:0,
from prog.cc:6:
/usr/local/boost-1.60.0/include/boost/bind/bind.hpp: In instantiation of 'boost::_bi::result_traits<boost::_bi::unspecified, boost::arg<1> >':
/usr/local/boost-1.60.0/include/boost/bind/bind.hpp:1212:48: instantiated from 'boost::_bi::bind_t<boost::_bi::unspecified, boost::arg<1>, boost::_bi::list1<boost::_bi::value<const char*> > >'
prog.cc:32:81: instantiated from here
/usr/local/boost-1.60.0/include/boost/bind/bind.hpp:75:37: error: no type named 'result_type' in 'struct boost::arg<1>'
prog.cc:34:82: error: expected ')' before ';' token
prog.cc: In function 'int main()':
prog.cc:50:48: error: no match for call to '(const boost::function<int(myStruct_t*, std::basic_string<char>)>) (myStruct_t&)'
/usr/local/boost-1.60.0/include/boost/function/function_template.hpp:765:17: note: candidate is: result_type boost::function2<R, T1, T2>::operator()(T0, T1) const [with R = int, T0 = myStruct_t*, T1 = std::basic_string<char>, result_type = int]
看来我对boost::bind的使用方式感到困惑。有人可以帮我正确地做吗?非常感谢。
调用点预期的函数签名是 int(myStruct*)
因为这一行(我添加了 & 以消除逻辑错误):
int myInt = (iter->second)(&myStruct);
在这种情况下,地图的声明应该是:
typedef std::map<std::string, boost::function<int(myStruct_t*)> > Map_t;
Map_t myMap = boost::assign::map_list_of
("Jake", boost::bind(&foo, boost::placeholders::_1, std::string("A")))
("Ken", boost::bind(&foo, boost::placeholders::_1, std::string("B")))
("Pete", boost::bind(&foo, boost::placeholders::_1, std::string("C")));
然后就可以开始了。
解释:
boost[std]::bind returns 一个函数对象,旨在仅接受在 bind
表达式中作为占位符提及的参数。
这意味着最终调用的函数通常比 bind
因此在您的情况下,foo
具有以下签名:
int foo( myStruct_t* m, std::string str)
即接受两个参数和 returns 一个整数。
但是在调用 bind
时:
boost::bind(&foo, boost::placeholders::_1, std::string("A"))
我们所说的是“捕获函数 foo
和第二个参数(字符串)。Return 我是一个函数对象,需要一个参数 (_1
) 和将该参数作为第一个参数转发给 foo
,同时将绑定的字符串作为第二个参数传递。
如此给出:
auto f = boost::bind(&foo, boost::placeholders::_1, std::string("A"));
f
具有签名 int f(MyStruct*)
并且当用
调用时auto i = f(&mystruct);
相当于调用:
auto i = foo(&mystruct, std::string("A"));