我使用boost ::功能是这样的:的boost ::绑定的隐式转换提振::函数或函数指针
template<class T1>
void run(boost::function<void (T1)> func, string arg)
{
T1 p1 = parse<T1>(arg);
func(p1);
}
当这样使用,一切正常:
void test1(int i)
{
cout << "test1 i=" << i << endl;
}
...
boost::function<void (int)> f = &test1;
run(f, "42");
我希望能够将原始函数指针直接传递,所以我超载这样的run()函数:
template<class T1>
void run(void (*func)(T1), string arg)
{
T1 p1 = parse<T1>(arg);
(*func)(p1);
}
...
run(&test1, "42"); // this is OK now
现在,我想能够将boost :: bind的结果传递给run()函数。就像这样:
void test2(int i, string s)
{
cout << "test2 i=" << i << " s=" << s << endl;
}
...
run(boost::bind(&test2, _1, "test"), "42"); // Edit: Added missing parameter 42
但是这不会编译:编辑
bind.cpp: In function ‘int main()’:
bind.cpp:33:59: error: no matching function for call to ‘run(boost::_bi::bind_t<void, void (*)(int, std::basic_string<char>), boost::_bi::list2<boost::arg<1>, boost::_bi::value<std::basic_string<char> > > >, std::string)’
bind.cpp:33:59: note: candidates are:
bind.cpp:7:6: note: template<class T1> void run(boost::function<void(T1)>, std::string)
bind.cpp:14:6: note: template<class T1> void run(void (*)(T1), std::string)
我应该如何运行超载()接受的boost :: bind()的?
编辑2
我知道我能做到这一点是这样的:
boost::function<void (int)> f = boost::bind(&test2, _1, string("test"));
run(f, "42");
但我想使用是更简洁。
编辑3
更改的run()的原型来自run(boost::function<void (T1)>, T1)
到run(boost::function<void (T1)>, string)
阐述实际使用情况。参考。伊戈尔R.的回答
整个源文件可以得到here
没有“隐式投射”这样的事情。强制转换是您在源代码中编写的内容,以告知编译器进行转换。编译器可以在没有转换的情况下进行转换;这种转换是“隐式转换”。当你使用强制转换时,它是一个“显式转换”。 – 2013-04-09 10:03:10
确实。问题标题应该改变。 – anorm 2013-04-09 11:05:42