The following code give compile error caused by line 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);
}
The question is, what the protoype of functor2 in line 17 should be?
If I really want to keep the functor2's prototype to be "boost::function<void()>, how to make the boost::bind return such a type?
The compilation error is:usr/include/boost/bind/bind.hpp:253: error: invalid initialization of reference of type 'void (&)()' from expression of type 'void (*)()'
What does it mean?
foo(T const& arg)
takes a reference argument. In order to pass a reference argument through boost::bind
, you need to wrap it with boost::ref
.
boost::function<T> functor2 = boost::bind(&A::foo<T>, boost::ref(arg));