【发布时间】:2016-03-22 16:46:27
【问题描述】:
为什么只有第一个实现有效,而不是下面指定的其他实现, 谁能解释这个模板结构的工作方式以及为什么其他人不这样做。
有效的模板结构
template <typename T, typename U>
struct is_same
{
static const bool value = false;
};
template <typename T>
struct is_same<T, T>
{
static const bool value = true;
};
不起作用的模板结构
template <typename T, typename U>
struct is_same<T, U>
{
static const bool value = false;
};
template <typename T>
struct is_same<T, T>
{
static const bool value = true;
};
还有一个不起作用
template <typename T, typename U>
struct is_same<T,U>
{
static const bool value = false;
};
template <typename T>
struct is_same
{
static const bool value = true;
};
还有主要功能
template <class A, class B>
bool IsSameClass() {
return is_same<A, B>::value;
}
int main()
{
bool ret = IsSameClass<P,C>();
}
【问题讨论】:
-
查看模板专业化。