如何在编译时测试 B 类是否派生自 std::vector?
template<class A>
struct is_derived_from_vector {
static const bool value = ????;
};
如何在编译时测试 B 类是否来自模板族?
template<class A, template< class > class Family>
struct is_derived_from_template {
static const bool value = ????;
};
使用:
template<class T> struct X {};
struct A : X<int> {}
struct B : std::vector<char> {}
struct D : X<D> {}
int main() {
std::cout << is_derived_from_template<A, X>::value << std::endl; // true
std::cout << is_derived_from_template<D, X>::value << std::endl; // true
std::cout << is_derived_from_vector<A>::value << std::endl; // false
std::cout << is_derived_from_vector<B>::value << std::endl; // true
}