C++ compile-time predicate to test if a callable object of type F can be called with an argument of type T

前端 未结 3 527
庸人自扰
庸人自扰 2020-12-31 04:18

I would like to create a compile-type function that, given any callable object f (function, lambda expression, function object, ...) and a type T,

3条回答
  •  佛祖请我去吃肉
    2020-12-31 05:18

    template
    struct is_callable_with_impl : std::false_type {};
    
    template
    struct is_callable_with_impl() (std::declval()) ) >::type
          > : std::true_type {};
    
    template
    constexpr bool is_callable_with(F &&) 
    { 
         return is_callable_with_impl< F, T >::value; 
    }
    

    It is basically the same solution as the one posted by Paul, I just prefer to use conditional instead of an holder class to avoid namespace pollution.

提交回复
热议问题