Inferring the call signature of a lambda or arbitrary callable for “make_function”

前端 未结 3 753
既然无缘
既然无缘 2020-11-28 09:01

In some situations it\'s desirable to be able to type-erase a callable (e.g. function, function pointer, object instance with operator(), lambda, mem_fn

3条回答
  •  误落风尘
    2020-11-28 09:31

    I've come up with a fairly nasty non-library solution, using the fact that lambdas have operator():

    template struct remove_class { };
    template
    struct remove_class { using type = R(A...); };
    template
    struct remove_class { using type = R(A...); };
    template
    struct remove_class { using type = R(A...); };
    template
    struct remove_class { using type = R(A...); };
    
    template
    struct get_signature_impl { using type = typename remove_class<
        decltype(&std::remove_reference::type::operator())>::type; };
    template
    struct get_signature_impl { using type = R(A...); };
    template
    struct get_signature_impl { using type = R(A...); };
    template
    struct get_signature_impl { using type = R(A...); };
    template using get_signature = typename get_signature_impl::type;
    
    template using make_function_type = std::function>;
    template make_function_type make_function(F &&f) {
        return make_function_type(std::forward(f)); }
    

    Any ideas where this can be simplified or improved? Any obvious bugs?

提交回复
热议问题