类模板的全特化和偏特化

来源:互联网 发布:node pm2 全局安装 编辑:程序博客网 时间:2024/04/27 17:35
#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;}

0 0
原创粉丝点击