Is it possible to figure out the parameter type and return type of a lambda?

后端 未结 4 1813
[愿得一人]
[愿得一人] 2020-11-22 02:08

Given a lambda, is it possible to figure out it\'s parameter type and return type? If yes, how?

Basically, I want lambda_traits which can be used in fol

4条回答
  •  被撕碎了的回忆
    2020-11-22 02:29

    The specialization method shown in @KennyTMs answer can be extended to cover all cases, including variadic and mutable lambdas:

    template 
    struct closure_traits : closure_traits {};
    
    #define REM_CTOR(...) __VA_ARGS__
    #define SPEC(cv, var, is_var)                                              \
    template                         \
    struct closure_traits                  \
    {                                                                          \
        using arity = std::integral_constant;   \
        using is_variadic = std::integral_constant;              \
        using is_const    = std::is_const;                             \
                                                                               \
        using result_type = R;                                                 \
                                                                               \
        template                                                \
        using arg = typename std::tuple_element>::type; \
    };
    
    SPEC(const, (,...), 1)
    SPEC(const, (), 0)
    SPEC(, (,...), 1)
    SPEC(, (), 0)
    

    Demo.

    Note that the arity is not adjusted for variadic operator()s. Instead one can also consider is_variadic.

提交回复
热议问题