類模板的全特化和偏特化
阿新 • • 發佈:2019-02-07
#include <stdio.h> #include <iostream> template<typename T1,typename T2> class A { public: A(T1 self_str1,T2 self_str2) { printf("類模板:\n"); std::cout << self_str1 << std::endl; std::cout << self_str2 << std::endl; printf("\n"); } ~A() { std::cout << "~A" << std::endl; } }; //函式模板的偏特化 template<typename T2> class A<double,T2> { public: A(double t1,T2 t2) { printf("類模板偏特化:\n"); printf("double:%f\n", t1); std::cout << "T2:"<<t2 << std::endl; printf("\n"); } ~A() { std::cout << "~A:double" << std::endl; } }; template<> class A<double, double> { public: A(double t1, double t2) { printf("類模板全特化:\n"); printf("double:%f %f\n", t1,t2); printf("\n"); } ~A() { std::cout << "~A:double" << std::endl; } }; int main() { A<int,int> a(5,6); //使用類模板 A<double,char *> b(5.0,"hello world");//使用偏特化 A<double,double> c(0.4,0.5); //使用全特化 return 0; }