下面的代码给出了第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的原型(prototype)应该是什么?
如果我真的想保持 functor2 的原型(prototype)为“boost::function
编译错误是:usr/include/boost/bind/bind.hpp:253: error: invalid initialization of reference of type 'void (&)()' from expression of type 'void (*) ()'
什么意思?
最佳答案
foo(T const& arg)
采用引用参数。为了通过 boost::bind
传递引用参数,您需要用 boost::ref
包装它。
boost::function<T> functor2 = boost::bind(&A::foo<T>, boost::ref(arg));
关于c++ - double boost::bind 导致编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112761/