【发布时间】:2012-04-11 19:38:51
【问题描述】:
以下代码给出了第 17 行导致的编译错误:
#include <boost/bind.hpp>
#include <boost/function.hpp>
void func()
{}
class A{
public:
template <typename T>
static void foo(T const& arg){}
template <typename T>
void bar(T const& arg){
boost::bind(&A::foo<T>, arg); //OK
boost::function<T> functor1 = boost::bind(func); //OK
boost::function<T> functor2 = boost::bind(&A::foo<T>, arg); //ERROR, LINE 17
}
};
int main()
{
A obj1;
obj1.bar(func);
}
问题是,第 17 行的 functor2 原型应该是什么?
如果我真的想保持functor2的原型为“boost::function
编译错误为:usr/include/boost/bind/bind.hpp:253: error: invalid initialization of reference of type 'void (&)()' from expression of type 'void (*)()'
什么意思?
【问题讨论】:
-
试试 boost::function
functor2 = boost::bind(&A::foo , arg); -
那行不通。因为 T 已经是“void(*)()”了。
标签: c++ boost bind functor boost-bind