Finding out the return type of a function, lambda or function

前端 未结 2 912
醉梦人生
醉梦人生 2021-01-18 18:26

This seems to be solved for the case of lambdas in this question. But that one is a 2011 answer and I\'m looking for a general case: lambdas, regular functions, and functors

2条回答
  •  生来不讨喜
    2021-01-18 18:53

    Assuming that:

    1. You want only the return type.
    2. You don't know what are/will be the types of arguments (so neither decltype() nor std::result_of<> is an option.
    3. The functor object passed as argument will not have an overloaded or generic operator().

    then you can use the below trait that infers the return type of any functor object:

    template 
    struct return_type_impl;
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type_impl { using type = R; };
    
    template 
    struct return_type
        : return_type_impl {};
    
    template 
    struct return_type
        : return_type_impl {};
    
    template 
    using return_type_t = typename return_type::type;
    

    Tests:

    #include 
    
    template 
    void test(F h)
    {
        static_assert(std::is_same, int>{}, "!");
    
        return_type_t i = 1;
    }
    
    int function(int i) { return 2*i; }
    
    int c_varargs_function(...) { return 1; }
    
    struct A
    {
        int mem_function(double, float) { return 1; } 
    };
    
    int main()
    {
        // Function
        test(function);
    
        // C-style variadic function
        test(c_varargs_function);
    
        // Non-generic lambda
        test([](int i) { return 2*i; });
    
        // Member function
        test(&A::mem_function);
    }
    

    DEMO

提交回复
热议问题