gpt4 book ai didi

c++ - 如何使用函数指针映射正确使用 boost::bind

转载 作者:行者123 更新时间:2023-11-30 03:41:47 24 4
gpt4 key购买 nike

我正在尝试实现类似的目标,但遇到了与 boost bind 相关的错误

#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);

在这种情况下, map 的声明应该是:

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)

即接受两个参数并返回一个 int。

但是在调用 bind 时:

boost::bind(&foo, boost::placeholders::_1, std::string("A"))

我们所说的是“捕获函数 foo 和第二个参数(一个字符串)。返回一个需要一个参数的函数对象(_1)和将该参数作为第一个参数转发给 foo,同时将绑定(bind)的字符串作为第二个参数传递。

如此给出:

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"));

关于c++ - 如何使用函数指针映射正确使用 boost::bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37162032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com