模版检查类中是否有某个成员函数

//例子1
template<typename T>
struct has_no_destroy
{
     
  template <typename C> static char test(decltype(&C::no_destroy));
  template <typename C> static int32_t test(...);
  const static bool value = sizeof(test<T>(0)) == 1;
};
 
//例子2
 
template<typename T, typename Sig>                                 
struct has_foo {
                          
    template <typename U, U> struct type_check;
    template <typename V> static char (& chk(type_check<Sig, &V::foo>*))[1];
    template <typename  > static char (& chk(...))[2]; 
    static bool const value = (sizeof(chk<T>(0)) == 1);
};

template <typename Type> 
class has_foo
{
      
   class yes {
      char m;}; 
   class no {
      yes m[2];}; 
   struct BaseMixin 
   {
      
     void foo(){
     } 
   }; 
   struct Base : public Type, public BaseMixin {
     }; 
   template <typename T, T t>  class Helper{
     }; 
   template <typename U> 
   static no deduce(U*, Helper<void (BaseMixin::*)(), &U::foo>* = 0); 
   static yes deduce(...); 
public: 
   static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0))); 
}; 

你可能感兴趣的:(C/C++编程)