Operator overloading for lambdas?

后端 未结 2 1591
一整个雨季
一整个雨季 2020-12-19 20:53

I have asked a similar question before overloading operator >> for lambdas
But i did not explained what i really wanted .

I am writing a simple wrapper around

2条回答
  •  梦毁少年i
    2020-12-19 21:04

    What you need is a retrospective cast. A way to compose a correct function object type from passing it only a lambda (and nothing else, no template arguments, not return type specification).

    A way to do it without dependencies from other libraries would be the following

    #include 
    #include
    #include
    
    
    using namespace std;
    
    
    template
    struct memfun_type 
    {
        using type = void;
    };
    
    template
    struct memfun_type
    {
        using type = std::function;
    };
    
    template
    typename memfun_type::type
    FFL(F const &func) 
    { // Function from lambda !
        return func;
    }
    

    Then you can be aware of your lambas' return type and write the following

    int main() 
    {
        database_bind dbb;
    
        dbb >> FFL([](int i, string s) { cout << i << ' ' << s << endl; });
        dbb >> FFL([](int i) { cout << i << endl; });
        dbb >> FFL([](string s,double d) { cout << s << ' ' << d << endl; });
    }
    

提交回复
热议问题