std :: is_base_of用于模板类 [英] std::is_base_of for template classes
问题描述
当 A
是模板类时,是否有一种方法来测试 std :: is_base_of< A,B& ?
Is there a way to test std::is_base_of<A, B>
when A
is a template class?
template <typename X, typename Y> class A {};
template <typename X> class B : public A<X, char> {};
我想静态测试类似于 std :: is_base_of< A, B< int>
含义, B
源自 A
的任何专门化。
(为了更一般,让我们说我们不知道 B
specializes A
的方式,即,B X源自A
I want to statically test something like, std::is_base_of<A, B<int>>
meaning, B
is derived from any specialization of A
.
(To make it more general, let's say we don't know the way B
specializes A
, i.e. B<X> derives from A<X, char>)
一种解决方法是从-template)class say C
,然后检查 std :: is_base_of< C,B< int>
。
One way to solve would be to derived A from a (non-template) class say C
, and then check std::is_base_of<C, B<int>>
. But is there another way to do this?
推荐答案
您可以执行以下操作:
template <template <typename...> class C, typename...Ts>
std::true_type is_base_of_template_impl(const C<Ts...>*);
template <template <typename...> class C>
std::false_type is_base_of_template_impl(...);
template <typename T, template <typename...> class C>
using is_base_of_template = decltype(is_base_of_template_impl<C>(std::declval<T*>()));
,但会失败,并会出现多个继承或私有继承从 A
。
but will fail with multiple inheritance or private inheritance from A
.
这篇关于std :: is_base_of用于模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!