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(类模板二)的更多相关文章

  1. STL之template类模板

    #include <iostream> using namespace std; template<class T>//类模板 class Person{ public://构 ...

  2. C++ 类模板二(类模版与友元函数)

    //类模版与友元函数 #include<iostream> using namespace std; template<typename T> class Complex{ p ...

  3. c++类模板与其他

    static static的成员不再单独属于一个对象,他是单独的保存在内存的某个地址,也就只有一份.所以在设计程序的时候要看这个东西是不是只需要一份. static函数和一般的函数一样,在内存中只有一 ...

  4. C++ template学习二 类模板定义及实例化

    一个类模板(也称为类属类或类生成类)允许用户为类定义一种模式,使得类中的某些数据成员.默写成员函数的参数.某些成员函数的返回值,能够取任意类型(包括系统预定义的和用户自定义的). 如果一个类中数据成员 ...

  5. C++基础 (9) 第九天 编译器对模板类的二次编译 类模板 自定义数组类

    1 昨日回顾 2 编译器对于模板的二次编译 写一个模板函数 然后进行调用 g++ template.cpp -o template // 汇编 g++ -S template.cpp –o templ ...

  6. c++11-17 模板核心知识(二)—— 类模板

    类模板声明.实现与使用 Class Instantiation 使用类模板的部分成员函数 Concept 友元 方式一 方式二 类模板的全特化 类模板的偏特化 多模板参数的偏特化 默认模板参数 Typ ...

  7. 类模板 template<class T>

    参考网址:http://c.biancheng.net/cpp/biancheng/view/213.html // demo3.cpp : 定义控制台应用程序的入口点. // #include &q ...

  8. C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化

    模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...

  9. IntelliJ IDEA 2017版 使用笔记(五) 模板 live template自定义设置(二) ;postfix使用;IDE快捷键使用

    一.live template 活模板     就像这个单词的含义一样,live template就是一个高效的提高代码,书写速度的方式,(live template位置File-----settin ...

随机推荐

  1. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  2. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  3. See you~(hdu1892)

    See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Su ...

  4. P1629八

    P1629八 Accepted 标签:[显示标签]     描述 八是个很有趣的数字啊.八=发,八八=爸爸,88=拜拜.当然最有趣的还是8用二进制表示是1000.怎么样,有趣吧.当然题目和这些都没有关 ...

  5. WPF自定义界面WindowChrome

    WPF自定义界面WindowChrome 默认WPF的界面其实也还行,就是满足不了日渐增长的需求,界面还是需要有更高的自定义程度,包括标题栏也要能够塞下更多的操作控件. 默认窗口介绍 新建WPF项目, ...

  6. 团队编程二——web应用之人事管理系统

    该项目是B-S模式的web应用,以下是团队各成员的Coding链接: ------Aaric---https://coding.net/u/Aaric/p/Personnel_management_s ...

  7. 第二十九个知识点:什么是UF-CMA数字签名的定义?

    第二十九个知识点:什么是UF-CMA数字签名的定义? 第16篇博客给出了DSA,Schnoor和RSA-FDH签名方案的细节,但是签名方案是什么?它应该保证什么样的安全性? 一个签名方案\(S\)是一 ...

  8. Java Web程序设计笔记 • 【第9章 EL表达式】

    全部章节   >>>> 本章目录 9.1 EL 表达式基础 9.1.1 EL 表达式简介 9.1.2 EL 表达式的定义 9.1.3 使用 EL 访问变量 9.1.4 使用 E ...

  9. Java面向对象笔记 • 【第3章 继承与多态】

    全部章节   >>>> 本章目录 3.1 包 3.1.1 自定义包 3.1.2 包的导入 3.1.3 包的访问权限 3.1.4 实践练习 3.2 继承 3.2.1 继承概述 3 ...

  10. C# 执行Javascript脚本

    前一阵子使用C#编写SCXML状态机,需要解析EMCScript表达式,使用了Jint库(https://github.com/sebastienros/jint/),当时感觉与C#之间的数据转换不是 ...