将std :: function与回调lambda进行比较

时间:2016-07-01 14:08:45

标签: c++

继续线程Compare of std::function with lambda

我遇到了捕获this到lambda的问题 - 并在此之后与“正确的回调”进行比较。

#include <iostream>
#include <functional>
using namespace std;

//////////////////////////////////////////////////////////////////////////
class IFoo
{
public:
    virtual void onAppStarted( int, int ) = 0;
};
//////////////////////////////////////////////////////////////////////////
template <typename T>
struct FunctionTypeDeduction;

template<class ClassType, class ReturnType, class... Args>
struct FunctionTypeDeduction< ReturnType( ClassType::* )( Args... ) >
{
    using type                                  = ReturnType( *)( Args... );
    using functorType                           = std::function<ReturnType( Args... ) >;
};
//////////////////////////////////////////////////////////////////////////
class SubcribeSystem
{
public:
    using tSomeMethodType                       = FunctionTypeDeduction< decltype( &IFoo::onAppStarted ) >::type;
    using tSomeMethodCallback                   = FunctionTypeDeduction< decltype( &IFoo::onAppStarted ) >::functorType;

public:
    void subribeOnSomeMethod( const tSomeMethodCallback& arr )
    {
        arr( 3, 19 );
    }
};
#define SUBCRIBE_FOR_SOMEMETHOD( eventsObj, lambda )                    \
    do                                                                  \
    {                                                                   \
        SubcribeSystem::tSomeMethodType const func_ptr = lambda;        \
        eventsObj.subribeOnSomeMethod( lambda );                        \
    }                                                                   \
    while(false);                                                       
//////////////////////////////////////////////////////////////////////////
class Bar
{
public:
    void fn( SubcribeSystem& events )
    {
        auto onAppStart = []( int width, int height )
        {
            cout << width << " " << height << endl;
        };
        auto onAppStart2 = []( bool width, int height )
        {
            cout << width << " " << height << endl;
        };
        auto onAppStart3 = [this]( int width, int height )
        {
            cout << width << " " << height << endl;
            someOtherMethod( width );
        };
        SUBCRIBE_FOR_SOMEMETHOD( events, onAppStart );      // expect all ok    
        //SUBCRIBE_FOR_SOMEMETHOD( events, onAppStart2 );   // expect error cause bool first param  
        SUBCRIBE_FOR_SOMEMETHOD( events, onAppStart3 );     // expect all ok, but not !!!
    }
    void someOtherMethod( int x )
    {
        cout << "processed callback " << x << endl;
    }
};
int main()
{
    Bar bar;
    SubcribeSystem sub;
    bar.fn( sub );
} 

1 个答案:

答案 0 :(得分:1)

在你的宏中你有

SubcribeSystem::tSomeMethodType const func_ptr = lambda; 

SubcribeSystem::tSomeMethodType

的地方
FunctionTypeDeduction< decltype( &IFoo::onAppStarted ) >::type

这是一个函数指针。所以你试图将lambda转换为函数指针。不幸的是,onAppStart3捕获的lambda无法转换为函数指针。

前两个lambda工作,因为它们没有捕获,因此它们有一个隐式函数指针转换运算符。