I would like to realize a class Function similar to boost::function, the class Function can use like this in main.cpp :
#include <iostream>
#include "Function.hpp"
int funct1(char c)
{
std::cout << c << std::endl;
return 0;
}
int main()
{
Function<int (char)> f = &funct1;
Function<int (char)> b = boost::bind(&funct1, _1);
f('f');
b('b');
return 0;
}
In my Function.hpp, I have
template <typename T>
class Function;
template <typename T, typename P1>
class Function<T(P1)>
{
typedef int (*ptr)(P1);
public:
Function(int (*n)(P1)) : _o(n)
{
}
int operator()(P1 const& p)
{
return _o(p);
}
Function<T(P1)>& operator=(int (*n)(P1))
{
_o = n;
return *this;
}
private:
ptr _o; // function pointer
};
Above code works fine for Function f = &funct1,
but it can't work for Function b = boost::bind(&funct1, _1);
I wonder to know how exactly boost::Function works and What can I do to for my Function support boost::bind