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. SQL:利用多表更新优化子查询

    原SQL: update bi_data.order_list_wxset is_start='1',proc_time=now()where 1=1and is_end='0' and 交易时间&l ...

  2. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  3. Docker 与 K8S学习笔记(九)—— 容器间通信

    容器之间可通过IP.Docker DNS Server或joined三种方式进行通信,今天我们来详细学习一下. 一.IP通信 IP通信很简单,前一篇中已经有所涉及了,只要容器使用相同网络,那么就可以使 ...

  4. Docker 与 K8S学习笔记(七)—— 容器的网络

    本节我们来看看Docker网络,我们这里主要讨论单机docker上的网络.当docker安装后,会自动在服务器中创建三种网络:none.host和bridge,接下来我们分别了解下这三种网络: $ s ...

  5. Codeforces 849A:Odds and Ends(思维)

    A. Odds and Ends Where do odds begin, and where do they end? Where does hope emerge, and will they e ...

  6. Log4j 2.17.0 再曝漏洞,但不要惊慌!

    最新消息!根据Log4j官网发布,2.17.0版本还存在漏洞! 上图来自Log4j2官网:https://logging.apache.org/log4j/2.x/ 漏洞编号:CVE-2021-448 ...

  7. Electron-Vue 项目使用Element的el-table组件不显示

    1.electron-vue文件夹下的webpack.renderer.config.js配置文件,你会找到这样一行代码,需要添加白名单! let whiteListedModules = ['vue ...

  8. uniapp上传图片后台返回url后放入image中图片不显示问题

    原因:后台返回路径问题后台返回地址斜杠为 \ 我们需要的是 /  如下后台返回的地址导致转义了 修改后可以显示

  9. Java高级程序设计笔记 • 【第1章 IO流】

    全部章节   >>>> 本章目录 1.1 File类访问文件 1.1.1 File 类 1.1.2 File 类方法 1.1.3 实践练习 1.2 文件过滤器 1.2.1 Fi ...

  10. Mysql 设计超市经营管理系统,包括员工库存表(stock) 和 仓库表(warehouse)

    互联网技术学院周测机试题(三) 一.需求分析 为进一步完善连锁超市经营管理,提高管理效率,减少管理成本,决定开发一套商品管理系统,用于日常的管理.本系统分为商品管理.员工管理.店铺管理,库存管理等功能 ...