如何确定类型是否派生自模板类?特别是,我需要确定模板参数是否将 std::basic_ostream
作为基类。通常 std::is_base_of
是完成这项工作的工具。但是,std::is_base_of
仅适用于完整类型,不适用于类模板。
我正在寻找这样的东西。
template< typename T >
bool is_based_in_basic_ostream( T&& t )
{
if( std::is_base_of< std::basic_ostream< /*anything*/>, T >::value )
{
return true;
}
else
{
return false;
}
}
我确信这是可以做到的,我想不出怎么做。
最佳答案
我不知道有什么简洁明了的方法。但是你可以再次滥用重载
template< typename T, typename U >
std::true_type is_based_impl( std::basic_ostream<T, U> 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;
}
它只会检测公共(public)继承。请注意,您可以改为检测来自 ios_base
的派生,这可能同样适用于您(此测试对于输入流也将是积极的,因此它的适用性有限)
std::is_base_of<std::ios_base, T>
关于c++ - 如何判断一个类型是否派生自模板类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5997956/