2011-12-17 56 views
1

我想实现类似的一类功能,提升了::功能,类功能可以在main.cpp中使用这样的:的boost ::功能一样类

#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 ::绑定(& funct1,_1);
我不知道如何确切boost :: Function的作品,我能做些什么来为我的功能支持增强::绑定

+1

你是否意识到boost :: function和boost :: bind实际上是纯粹的魔法。重现其功能将是一件非常难以做到的事情。 – Lalaland 2011-12-17 12:53:40

+0

@EthanSteinberg:'boost :: function'是一个简单的类型擦除应用程序,并不是很神奇。 – Mankarse 2011-12-17 12:56:25

+1

@Mankarse我认为/usr/include/boost/bind/bind.hpp是1751行本身就说明了这一点。 – Lalaland 2011-12-17 12:59:17

回答