C++Template(类模板二)
namespace _myspace{ template<typename T, typename U> class TC { public: TC() { cout << "TC()的泛化版本" << endl; } void testone() { cout << "泛化版本函数:void testone" << endl; } };}int main(int argc,char** argv[]){ _myspace::TC<int,float> mytcone; mytcone.testone();
while (1); return 0;}
输出
上面的代码毋庸置疑执行泛化版本。
接下来改进:
加入全特化版本:
namespace _myspace
{
template<typename T, typename U>
class TC
{
public:
TC()
{
cout << "TC()的泛化版本" << endl;
}
void testone()
{
cout << "泛化版本函数:void testone" << endl;
}
};
template<>
class TC<int, float>
{
public:
TC()
{
cout << "TC()的全特化版本" << endl;
}
void testone()
{
cout << "全特化版本函数:void testone" << endl;
}
};
}
int main(int argc,char** argv[])
{
_myspace::TC<int,float> mytcone;
mytcone.testone(); while (1);
return 0;
}
输出:
接下来加入偏特化版本:
namespace _myspace
{
template<typename T, typename U>
class TC
{
public:
TC()
{
cout << "TC()的泛化版本" << endl;
}
void testone()
{
cout << "泛化版本函数:void testone" << endl;
}
};
template<>
class TC<int, float>
{
public:
TC()
{
cout << "TC()的全特化版本" << endl;
}
void testone()
{
cout << "全特化版本函数:void testone" << endl;
}
}; template<typename U>
class TC<float, U>
{
public:
TC()
{
cout << "TC()的偏特化版本" << endl;
}
void testone()
{
cout << "偏特化版本函数:void testone" << endl;
}
}; }
int main(int argc,char** argv[])
{
_myspace::TC<float,float> mytcone;
mytcone.testone(); while (1);
return 0;
}
输出:
以上是对模板参数数量的特化,接下来是模板参数范围的偏特化:
比如int向const int,T向T*转型类型的范围就变小了,所以这种方式也可以进行特化:
namespace _myspace
{
template<typename T, typename U>
class TC
{
public:
TC()
{
cout << "TC()的泛化版本" << endl;
}
void testone()
{
cout << "泛化版本函数:void testone" << endl;
}
};
template<>
class TC<int, float>
{
public:
TC()
{
cout << "TC()的全特化版本" << endl;
}
void testone()
{
cout << "全特化版本函数:void testone" << endl;
}
}; template<typename U>
class TC<float, U>
{
public:
TC()
{
cout << "TC()的偏特化版本" << endl;
}
void testone()
{
cout << "偏特化版本函数:void testone" << endl;
}
};
template<typename T,typename U>
struct TC<const T, U*>
{
TC()
{
cout << "TC(const T,U*)" << endl;
}
void testone()
{
cout << "TC(const T,U*):void testone" << endl;
}
}; }
int main(int argc,char** argv[])
{
_myspace::TC<const int,float*> mytcone;
mytcone.testone(); while (1);
return 0;
}
输出:
以上是对类的特化,接下来谈一些细节的东西:
回到以上代码:
我对成员函数进行特化:
输出:
如果加入静态函数:
输出:
注意对于以上的成员函数的特化和加入了静态函数,那么就不能对类进行相对应的特化和偏特化:
比如下面这样就不行:
以上时注意点,最后,如果想要在类外定义相应的函数,那么template可以不用写,当成实例化的类看待即可:
如下图:
C++Template(类模板二)的更多相关文章
- STL之template类模板
#include <iostream> using namespace std; template<class T>//类模板 class Person{ public://构 ...
- C++ 类模板二(类模版与友元函数)
//类模版与友元函数 #include<iostream> using namespace std; template<typename T> class Complex{ p ...
- c++类模板与其他
static static的成员不再单独属于一个对象,他是单独的保存在内存的某个地址,也就只有一份.所以在设计程序的时候要看这个东西是不是只需要一份. static函数和一般的函数一样,在内存中只有一 ...
- C++ template学习二 类模板定义及实例化
一个类模板(也称为类属类或类生成类)允许用户为类定义一种模式,使得类中的某些数据成员.默写成员函数的参数.某些成员函数的返回值,能够取任意类型(包括系统预定义的和用户自定义的). 如果一个类中数据成员 ...
- C++基础 (9) 第九天 编译器对模板类的二次编译 类模板 自定义数组类
1 昨日回顾 2 编译器对于模板的二次编译 写一个模板函数 然后进行调用 g++ template.cpp -o template // 汇编 g++ -S template.cpp –o templ ...
- c++11-17 模板核心知识(二)—— 类模板
类模板声明.实现与使用 Class Instantiation 使用类模板的部分成员函数 Concept 友元 方式一 方式二 类模板的全特化 类模板的偏特化 多模板参数的偏特化 默认模板参数 Typ ...
- 类模板 template<class T>
参考网址:http://c.biancheng.net/cpp/biancheng/view/213.html // demo3.cpp : 定义控制台应用程序的入口点. // #include &q ...
- C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化
模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...
- IntelliJ IDEA 2017版 使用笔记(五) 模板 live template自定义设置(二) ;postfix使用;IDE快捷键使用
一.live template 活模板 就像这个单词的含义一样,live template就是一个高效的提高代码,书写速度的方式,(live template位置File-----settin ...
随机推荐
- Spring Security Filter 学习笔记
过滤器可以简单理解成用于拦截请求,并执行相应逻辑的代码. 在Spring Security架构中实现过滤器 在SpringSecurity中,可以通过实现 javax.servlet 包中的 Filt ...
- RuoYi项目整合Mybatis-Plus 框架
RuoYi框架默认使用的是Mybatis框架 但是有的习惯使用MP框架,这就很不方便, 不过可以简单进行整合 引入依赖 <dependency> <groupId>com.ba ...
- google protobuf学习笔记:windows下环境配置
欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/45371743 protobuf的使用和原理,请查看:http:/ ...
- 【LeetCode】884. Uncommon Words from Two Sentences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetc ...
- 【剑指Offer】数组中出现次数超过一半的数字 解题报告(Python)
[剑指Offer]数组中出现次数超过一半的数字 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-inter ...
- 【操作系统】编程模拟FIFO,LRU,NUR,OPT页面置换算法
#include<stdio.h> #include<stdlib.h> #include<time.h> #define random(x) (rand()%x) ...
- 「THUSCH 2017」大魔法师
Description 大魔法师小 L 制作了 \(n\) 个魔力水晶球,每个水晶球有水.火.土三个属性的能量值.小 L 把这 \(n\) 个水晶球在地上从前向后排成一行,然后开始今天的魔法表演. 我 ...
- Java初学者作业——为某超市设计管理系统,需要在控制台展示系统菜单,菜单之间可以完成跳转。
返回本章节 返回作业目录 需求说明: 为某超市设计管理系统,需要在控制台展示系统菜单,菜单之间可以完成跳转. 实现思路: 定义mainMenu方法,用于显示主菜单. 主菜单主要负责显示4个选项,分别是 ...
- Ditto剪贴板增强工具
1.简介 Ditto是一款强大的Windows剪贴板增强工具,它支持64位操作系统,而且完全免费,绿色开源,支持中文,而且还有免安装的绿色版本. 开启Ditto后,不会有任何程序界面出现,它只是默默地 ...
- Unity——火烧+水波纹效果(噪音图)
使用噪声图实现火烧和水波纹效果: 1.溶解 关闭裁剪,根据noise纹理取样,r通道和_BurnAmount比较,裁剪掉小于_BurnAmount的片元: 通过菲尼尔得到裁剪边缘,添加火焰燃烧的颜色进 ...