template-模板完全特化
2017-09-13 20:29
375 查看
类模板的定义与使用
函数模板的完全特化
类模板的完全特化
#include <stdio.h> #include <typeinfo.h> template <class T1,class T2> class A { T1 i; T2 j; public: A(T1 t1,T2 t2) {i = t1; j = t2;}//内联函数 bool comp(){return i>j;}//内联函数 void print_Type(); }; //成员函数print_Type定义 template<class T1,class T2> void A<T1,T2>::print_Type() { printf("i的类型是:%s\n",typeid(i).name()); //打印i的类型名 printf("j的类型是:%s\n",typeid(j).name()); //打印j的类型名 } int main() { A<int,double> a(3,34.6); if(a.comp() ) printf("i>j \n"); else printf("i<j \n"); a.print_Type() ; return 0; }
函数模板的完全特化
#include <stdio.h> template <class T> void func(T a) { printf("hello \n"); } template<> void func<int>(int a)//函数模板完全特化 { printf("hello there\n"); } int main() { func(2); func('y'); return 0; } //输出: //hello there //hello
类模板的完全特化
#include <stdio.h> template <class T> class A { T i; public: A(T t){ i = t; printf("hello,%f\n", i); } T compute() { return i*i; } }; template<> class A<int>//类模板A完全模板化 { int i; int k; public: A(int t){ i = t; printf("hello,%d\n",i); } int compute(){ return i*i*i; } }; int main() { A<double> dObj(2.5); A<int> iObj(5);//打印hello printf("%f\n", dObj.compute());//平方 printf("%d\n", iObj.compute());//立方 return 0; }
相关文章推荐
- C++ 函数模板的特化(Function Template Specialization)
- 模板完全特化,函数重载的重载,类模板的继承
- Linux C/C++ 模板:主模板、完全特化
- 【C++模板】特化与偏特化 template [partial] specialization
- Linux C/C++ 模板:主模板、完全特化、局部特化
- 模板完全特化和部分特化
- 【C++模板】特化与偏特化 template [partial] specialization
- C++ template —— 模板中的名称(三)
- WPF 深入浅出 模板(DataTemplate 数据外衣、ControlTemplate 控件外衣、ItemsPanelTemplate 项布局
- C++ template模板的学习
- C++类模板的三种特化
- C++ 完全高精度模板 (hdu 4762)
- Python - 定制语法的string模板(template) 详解
- Asp.net2.0 DataGrid多行表头与可编辑template模板的网格实现。
- template template parameter模板模板参数笔记----C++学习之路
- C++中 模板Template的使用
- C++ - 函数模板定制(function template specialization) 详解 及 代码
- vim模板插件vim-template的使用
- WPF学习笔记(1):样式(Style)与模板(Template)
- 行为模式之Template(模板模式)