检查一个类是否有方法

时间:2010-09-01 12:54:18

标签: c++ templates boost metaprogramming

  

可能重复:
  Possible for C++ template to check for a function's existence?

你知道C ++中的任何元编程技巧,我可以检查一个类是否有特定的方法。我一直在考虑这样的事情:

template <class T, class Enable = void> 
class A { ... };

// This version of the class will be compiled if T doesn't have my_method
template <class T>
class A<T, typename enable_if<has_method<T, T::my_method> >::type> { ... };

// This version of the class will be compiled if T has my_method
template <class T>
class A<T, typename disable_if<has_method<T, T::my_method> >::type> { ... };

其中一个选项是在其他人可以继承的类中定义my_method,然后使用is_base_of,但是其他选项是否需要继承?

0 个答案:

没有答案
相关问题