模板的特化与偏特化

/*
 * special_2.cpp
 *
 *  Created on: 2013年8月4日
 *      Author: Administrator
 */

#include <iostream>
#include <typeinfo>
#include <string>

using namespace std;
template <typename T>
class Type{
public:
	static string name(){
		return typeid(T).name();
	}
};

//特化
template <>
class Type<bool>{
public:
	static string name(){
		return "bool  !!!";
	}
};

template <>
class Type<int>{
public:
	static string name(){
		return "int ~~~~";
	}
};


template <>
class Type<double>{
public:
	static string name(){
		return "double ...";
	}
};

template <>
class Type<char>{
public:
	static string name(){
		return "char  ";
	}
};

//偏特化
template <typename T>
class Type<T*>{
public:
	static string name(){
		return Type<int>::name() + " pointer";
	}
};

int main(){
	cout<<Type<double>::name()<<endl;
	cout<<Type<int>::name()<<endl;
	cout<<Type<char>::name()<<endl;
	cout<<Type<int*>::name()<<endl;
	cout<<Type<bool>::name()<<endl;
}


原文地址:https://www.cnblogs.com/dyllove98/p/3236912.html