1. 判断两个类型是否相同
template<class T1, class T2>
class my_is_same {
public:
operator bool() {
return false;
}
};
template<class T1>
class my_is_same<T1, T1> {
public:
operator bool() {
return true;
}
};
2. 判断某个类型是否为指定类型
// 就以int为例吧
template<class T>
class my_is_int {
public:
operator bool() {
return false;
}
};
template<>
class my_is_int<int> {
public:
operator bool() {
return true;
}
};
其实还可以写出其他版本,原理都是类似的。