Operator overloading for lambdas?

后端 未结 2 1598
一整个雨季
一整个雨季 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条回答
  •  余生分开走
    2020-12-19 21:13

    Based on this question i wrote the fallowing code.
    I did not completely understood function_traits ! but i was able to overload the >> operator for lambdas with different number of arguments . I know it's not the best possible solution , but i wrote it so someone can take it as a starting point ( a variadic template implementation will be awesome !) .

    #include
    #include
    #include
    using namespace std;
    
    template 
    struct function_traits
    : public function_traits
    {};
    
    template 
    struct function_traits
    // we specialize for pointers to member function
    {
        enum { arity = sizeof...(Args) };
        // arity is the number of arguments.
    
        typedef ReturnType result_type;
    
        template 
        struct arg
        {
            typedef typename std::tuple_element>::type type;
            // the i-th argument is equivalent to the i-th tuple element of a tuple
            // composed of those arguments.
        };
    };
    
    // a place holder class
    class database_bind {};
    
    template
    class A {
        template
        static void run(F l);
    };
    
    template<>
    struct A<1> {
        template
        static void run(F l) {
            typedef function_traits traits;
            typedef typename traits::arg<0>::type type_1;
    
            type_1 col_1;
            get_from_db(0, col_1);
    
        l(col_1);
        }
    };
    template<>
    struct A<2> {
        template
        static void run(F l) {
            typedef function_traits traits;
            typedef typename traits::arg<0>::type type_1;
            typedef typename traits::arg<1>::type type_2;
    
            type_1 col_1;
            type_2 col_2;
            get_from_db(0, col_1);
            get_from_db(1, col_2);
    
            l(col_1, col_2);
        }
    };
    
    
    void get_from_db(int col_inx, string& str) {  str = "string"; }
    void get_from_db(int col_inx, int& i) {i = 123;}
    void get_from_db(int col_inx, double& d) { d = 123.456; }
    
    
    template
    void operator>>(database_bind dbb, F l)
    {
        typedef function_traits traits;
        A::run(l);
    }
    

    And finally :

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

提交回复
热议问题