从下面的例子可以大概看出来,
编译器处理模板的时候,只看模板的名称,
不关注模板的继承和实现
#include <iostream>
struct Ca {};
struct Cb {};
template<typename t,typename t2=void>
struct Cc :Ca {
Cc() { std::cout << "x1" << std::endl; }
};
//特化还可以改变继承关系呢?
template<typename t>
struct Cc<t*, typename std::enable_if<std::is_same<t, int>::value>::type>: Cb{
Cc() { std::cout << "x2" << std::endl; }
};
//! Trait yielding true if class T1 is base of T2 but not the same
template <class T1, class T2, class Dummy = void>
struct is_base_but_not_same : std::is_base_of<T1, T2>
{
};
//! Explicit specialization of is_base_of trait to workaround the
//! requirement of type to be complete when T1 and T2 are the same.
template <class T1, class T2>
struct is_base_but_not_same<T1,
T2,
//如果不相等就返回到通用模板,还有一种情况是如果type没有满足的也会,选择其他通用的模板
typename std::enable_if<std::is_same<T1, T2>::value>::type>
: std::false_type
{
};
int main()
{ //Ca可以是Ca的基类
//static_assert(std::is_base_of<Ca,Ca>::value , "Dummy");
////严格相等才算相等
////static_assert(std::is_same<Cb,Ca>::value , "Dummy");
////
//static_assert(is_base_but_not_same<Ca,Cb>::value, "");
// std::cout << "Hello World!\n";
Cc<void*> c1; //x1
Cc<int*> c2; //x2
}
原文:https://www.cnblogs.com/iwetuan/p/14361285.html