如何确定类型是否派生自模板类?特别是,我需要确定模板参数是否具有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;
}
它只会检测公共继承。请注意,您可以改为检测派生自ios_base
,这可能同样适合您(此测试对于输入流也将是积极的,因此它的适用性有限)
std::is_base_of<std::ios_base, T>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)