C++ templates: how to determine if a type is suitable for subclassing

前端 未结 1 1109
故里飘歌
故里飘歌 2020-12-19 00:13

Let\'s say I have some templated class depending on type T. T could be almost anything: int, int*, pair

相关标签:
1条回答
  • 2020-12-19 01:04

    You want to determine whether it is a non-union class. There is no way known to me to do that (and boost hasn't found a way either). If you can live with union cases false positives, you can use a is_class.

    template<typename> struct void_ { typedef void type; };
    
    template<typename T, typename = void>
    struct is_class { static bool const value = false; };
    
    template<typename T>
    struct is_class<T, typename void_<int T::*>::type> { 
      static bool const value = true; 
    };
    

    Boost has an is_union that uses compiler-specific builtins though, which will help you here. is_class (which boost also provides) combined with is_union will solve your problem.

    0 讨论(0)
提交回复
热议问题