Can we get the type of a lambda argument?

后端 未结 2 858
情深已故
情深已故 2020-12-05 00:45

Using std::function, we can get the type of an argument using the argument_type, second_argument_type etc. typedefs, but I can\'t see

2条回答
  •  执笔经年
    2020-12-05 01:17

    @Luc's answer is great but I just came across a case where I also needed to deal with function pointers:

    template
    Arg first_argument_helper(Ret(*) (Arg, Rest...));
    
    template
    Arg first_argument_helper(Ret(F::*) (Arg, Rest...));
    
    template
    Arg first_argument_helper(Ret(F::*) (Arg, Rest...) const);
    
    template 
    decltype(first_argument_helper(&F::operator())) first_argument_helper(F);
    
    template 
    using first_argument = decltype(first_argument_helper(std::declval()));
    

    This can be used on both functors and function pointers:

    void function(float);
    
    struct functor {
        void operator() (int);
    };
    
    int main() {
        std::cout << std::is_same, int>::value
                  << ", "
                  << std::is_same, int>::value 
                  << std::endl;
        return 0;
    
    }
    

提交回复
热议问题