boost :: functionに似たクラスFunctionを実現したいと思います。クラスFunctionは、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;
}
私のFunction.hppには、
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
};
上記のコードは、関数f =&funct1では正常に機能
しますが、関数b = boost :: bind(&funct1、_1);では機能しません。
boost :: Functionがどのように正確に機能するのか、そしてFunctionサポートboost::bindのために何ができるのか知りたいです。