How to determine if a type is derived from a template class?

前端 未结 2 1386
太阳男子
太阳男子 2021-02-13 16:05

How can I determine if a type is derived from a template class? In particular, I need to determine if a template parameter has std::basic_ostream as a base class.

2条回答
  •  耶瑟儿~
    2021-02-13 16:52

    I'm not aware of a short and concise way. But you can abuse overloading again

    template< typename T, typename U >
    std::true_type is_based_impl( std::basic_ostream const volatile& );
    std::false_type is_based_impl( ... );
    
    template< typename T >
    bool is_based_in_basic_ostream( T&& t ) {
      return decltype(is_based_impl(t))::value;
    }
    

    It will only detect public inheritance. Note that you can instead detect derivation from ios_base, which may work for you equally well (this test will also be positive for input streams, so it's only of limited applicability)

    std::is_base_of
    

提交回复
热议问题