如何使用boost::与函数指针映射正确绑定
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返回一个函数对象,该对象被设计为只接受bind
表达式中作为占位符提及的参数。
这意味着最终被调用的函数通常比bind
返回的函数对象有更多的参数
因此,在您的情况下,foo
具有以下签名:
int foo( myStruct_t* m, std::string str)
即取两个自变量并返回一个整数
然而,在呼叫bind
:时
boost::bind(&foo, boost::placeholders::_1, std::string("A"))
我们所说的是"捕获函数foo
和第二个参数(字符串)。返回一个需要一个参数(_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"));
相关文章:
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 在基于范围的for循环中使用结构化绑定声明
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 视觉studo 2019中的漫画和静态/动态绑定
- 将自由函数绑定为类成员函数
- 将常量指针引用绑定到非常量指针
- 在无序映射的结构化绑定中推导类型
- C++映射中搜索元组错误:无法将"int"左值绑定到"int&&"
- 使用 boost :: 绑定将映射作为参数传递
- 如何使用boost::与函数指针映射正确绑定
- 映射/设置迭代器未解除可绑定运行时错误
- 使用 STL 的映射中最大的下限绑定
- OpenGL:着色器存储缓冲区映射/绑定
- 将 std 绑定传递到函数映射的问题
- 致命 Python 错误:无法使用C++绑定创建自动 TLSkey 映射
- std::映射绑定到所有键和值的堆的分配器
- 外部属性映射绑定到std::矢量在boost图形库
- 映射插入迭代器绑定