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

后端 未结 6 1825
清酒与你
清酒与你 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条回答
  •  猫巷女王i
    2021-02-04 13:42

    Ran across this while doing something else, was able to adapt my code to fit. It has the same features (and limitations) as @Xeo, but does not need sizeof trick/enable_if. The default parameter takes the place of needing to do the enable_if to handle template functions. I tested it under g++ 4.7 and clang 3.2 using the same test code Xeo wrote up

    #include 
    #include 
    
    namespace detail {
      template
      struct call_exact : std::false_type {};
    
      template struct ARGS { typedef void type; };
    
      template
      C * opclass(decltype(std::declval()(std::declval()...)) (C::*)(Args...)) { }
      template
      C * opclass(decltype(std::declval()(std::declval()...)) (C::*)(Args...) const) { }
    
      template
      struct call_exact,
        typename ARGS<
           decltype(std::declval()(std::declval()...)),
           decltype(opclass(&T::operator()))
         >::type
       > : std::true_type {};
    }
    
    template
    struct Callable : detail::call_exact> { };
    
    template
    struct Callable
     : Callable, Args...>{};
    

提交回复
热议问题