2011-12-17 57 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

回答