2013-10-17 91 views
6

我想弄清楚std :: function在与闭包结合使用时会发生什么。我现在还不能将自己的头包裹起来,例如:正在调用哪个构造函数? 任何人都可以发布一个minimalistic drop的工作示例来替换支持以下示例中所需功能的std :: function:c + +闭包和std ::功能

#include <functional> 

int main(int argc, char* argv[]) 
{ 
    int mybool = 5; 

    auto foo = [&] (int arg) { 
     return mybool * arg; 
    }; 

    std::function<int(int)> foo2 = foo; 

    int result = foo2(42); 

    return 0; 
} 
+2

一句话:魔术。尽管这只是模板代码,所以你可以简单地自己查看它。 –

+1

如果你发现这个有趣的话,['std :: bind'](http://en.cppreference.com/w/cpp/utility/functional/bind)会让你感到头晕目眩。 – WhozCraig

+0

除了模板之外,还有几个大型的C++库,它们是开源的(stdlibC++通常由GCC使用,而libC++通常由clang使用),因此您甚至可以查看非模板化代码。 –

回答

7

这里是简约的例子:

template <class F> 
struct Decomposer; 

template <class R, class A> 
struct Decomposer<R (A)> 
{ 
    typedef R return_type; 
    typedef A argument_type; 
}; 


template <class F> 
struct my_function 
{ 
    typedef typename Decomposer<F>::return_type return_type; 
    typedef typename Decomposer<F>::argument_type argument_type; 

    return_type operator() (argument_type arg) const { 
    return (*impl)(arg); 
    } 

    template <class From> 
    my_function(From &&from) 
    { 
    struct ConcreteImpl : Impl 
    { 
     typename std::remove_reference<From>::type functor; 
     ConcreteImpl(From &&functor) : functor(std::forward<From>(functor)) {} 
     virtual return_type operator() (argument_type arg) const override 
     { 
     return functor(arg); 
     } 
    }; 
    impl.reset(new ConcreteImpl(std::forward<From>(from))); 
    } 

private: 
    struct Impl { 
    virtual ~Impl() {} 
    virtual return_type operator() (argument_type arg) const = 0; 
    }; 

    std::unique_ptr<Impl> impl; 
}; 

的核心思想是利用类型擦除存储实际闭合不知道它的类型:看到虚拟Impl::operator()和本地定义的类型特异性持有人ConcreteImpl

Live example