How do I determine if a type is callable with only const references?

后端 未结 6 1814
清酒与你
清酒与你 2021-02-04 13:08

I want to write a C++ metafunction is_callable that defines value to be true, if and only if the type F has the function cal

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-04 13:42

    Something like this maybe? It's a bit round about to make it work on VS2010.

    template
    struct function_traits_impl;
    
    template
    struct function_traits_impl
    {
        typedef A1 arg1_type;
    };
    
    template
    struct function_traits_impl
    {
        typedef A1 arg1_type;
    };
    
    template
    struct function_traits_impl
    {
        typedef A1 arg1_type;
    };
    
    template
    typename function_traits_impl::arg1_type arg1_type_helper(T);
    
    template
    struct function_traits
    {
        typedef decltype(arg1_type_helper(&F::operator())) arg1_type;
    };
    
    template
    struct is_callable : public std::is_same::arg1_type, const Arg&>
    {
    }
    

提交回复
热议问题