Is an is_functor C++ trait class possible?

后端 未结 3 1769
一生所求
一生所求 2020-12-28 09:40

How can I deduce statically if an argument is a C++ function object (functor)?

template 
void test(F f) {}

I tried

3条回答
  •  青春惊慌失措
    2020-12-28 10:04

    template                                 
    struct is_functor 
    {                                                                   
        typedef char yes[1];                                            
        typedef char no [2];                                            
        template  struct type_check;                     
        template  static yes &chk(type_check*);
        template  static no  &chk(...);                    
        static bool const value = sizeof(chk(nullptr)) == sizeof(yes);     
    };
    

    Altered from this answer.

    It could be used like...

    template
    typename std::enable_if::value>::type func()
    {
    }
    

提交回复
热议问题