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 ...
随机推荐
- [Elasticsearch] ES 的Mapping 设计在实际场景中应用
背景 项目中有个需求是需要几个字段作为标签,统计各个标签的文档数量,同时支持分词后的全文检索功能. 原有的mapping设计: curl -XPUT http://ip:9200/meta_es_me ...
- HttpServletResponse工具类和HttpServletRequest工具类,前台参数接收方式和后台返回(JSON)数据格式
RequestUtils.java 操作类 package cn.utils; import org.apache.commons.lang3.StringUtils; import org.slf4 ...
- c++11之日期和时间库
本文主要介绍 std::chrono日期和时间用法. 演示环境: vs2017 0.头文件 1 #include <chrono> 2 #include <thread>// ...
- FilesCodingConvert--批量文件编码格式转换工具
FilesCodingConvert–批量文件编码格式转换工具 简介 最近开始学习使用Android Studio,因为它的方便易用,我打算以后就不在使用ADT的方式编写Android项目了.当从Ec ...
- 【LeetCode】900. RLE Iterator 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rle-itera ...
- Visible Trees(hdu2841)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4355:Party All the Time(三分模板)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- [CNKI]个人论文收录
[1]在校期间参加大创项目研究 以论文形式结题 发表时间:2018-03-25 基于VR虚拟现实技术的CBD微圈电商平台的研究 林旭; 陈丽娟 内江科技 2018-03-25 期刊 链接: 基于VR虚 ...
- JavaScript交互式网页设计 • 【第8章 jQuery动画与特效】
全部章节 >>>> 本章目录 8.1 显示隐藏动画效果 8.1.1 show() 方法与hide() 方法 8.1.2 toggle()方法 8.1.3 实践练习 8.2 ...
- 一种适合于MC与SMC算法的哈希表设计
MC算法与SMC算法中的三角片焊接问题 在之前的关于MC算法与SMC算法的博文中介绍了算法的实现,文章主要围绕算法的核心问题,即三角片如何产生的问题进行了详细的描述.但由于实际应用中需要的等值面Mes ...