当前位置:网站首页>模板进阶(跑路人笔记)
模板进阶(跑路人笔记)
2022-07-26 17:51:00 【就一个挺垃圾的跑路人】
非类型模板参数
模板形参分为两种
类型模板参数: 及跟在class或typename后面的参数类型名称
非类型模板参数: 就是用一个常量作为类(函数)模板的一个参数,在类(函数)模板中可将该参数当成常量来使用
这里的N是常数, 是可以作为数组的初始化长度的存在.

注意:
- 浮点数, 类对象和字符串是不允许作为非类型模板参数的.
- 非类型模板参数必须在编译器就可以确定结果.
模板的特化
我们在使用模板的时候有可能需要对一些固定的类型做特殊处理不然会使代码出现问题.
比如我们实现一个less函数作为大小的比较.

此时就需要对模板进行特化.
及针对特殊的类型进行特殊的函数实现操作.模板特化分为类模板特化函数模板特化
其中函数模板特化 其实大部分都是可以通过实例化实现的方式来解决.
所以模板的特化我们一般用于类上面.
全特化
如有以下代码:
template<class T1,class T2>
class test
{
public:
test()
{
cout << "test()T1 T2" << endl;
}
};
template<>
class test<char,int>
{
public:
test()
{
cout << "test()char int" << endl;
}
};
int main()
{
test<int, int>();
test<char, int>();
return 0;
}

执行结果如上图:
这里我们的test类就通过特化实现了当类需要对如char int 类型进行的特殊化处理.
这种特化我们称为全特化.及将所有的模板参数进行了特化.
偏特化
如有以下代码:
template<class T1,class T2>
class test
{
public:
test()
{
cout << "test()T1 T2" << endl;
}
};
偏特化可以分为两种特化
- 部分特化
template<class T1>
class test<T1 ,char>
{
public:
test()
{
cout << "test()T1 char" << endl;
}
};
如上代码就是将当T2类型为char的时候转入的.
- 类型限制特化
我们也可以指定一种类型进行特化如指针,引用.
代码如下:
template<class T1, class T2>
class test<T1*,T2*>//指针的特化
{
public:
test()
{
cout << "test()<T1*,T2*>" << endl;
}
};
template<class T1, class T2>//引用类型的特化
class test<T1&, T2&>
{
public:
test()
{
cout << "test<T1&, T2&>" << endl;
}
};
template<class T1, class T2>//引用加指针
class test<T1&, T2*>
{
public:
test()
{
cout << "test<T1&, T2*>" << endl;
}
};
边栏推荐
- VTK (the Visualization Toolkit) loads STL models
- Meta Cambria handle exposure, active tracking + multi tactile feedback scheme
- Multi merchant mall system function disassembly Lecture 16 - platform side member growth value record
- 详细介绍@GetMapping和@PostMapping的区别
- 14. Gradient detection, random initialization, neural network Summary
- 第一个ABAP ALV报表程序构建流程
- The first day of Oracle (review and sort out the common knowledge points of development)
- Neural network learning (2) introduction 2
- 打印日志的一些小技巧
- Netease game R & D Engineer Intern (client side)
猜你喜欢

NFT数字藏品开发:数字藏品助力企业发展

Flex layout

Have you ever encountered a deadlock problem in MySQL? How did you solve it?

14. Gradient detection, random initialization, neural network Summary

Meta Cambria handle exposure, active tracking + multi tactile feedback scheme

Redis主从复制,读写分离,哨兵模式

Vector canoe menu plugin getting started

NFT数字藏品系统开发:上线即售罄,网民“秒杀”数字藏品
![455. Distribute cookies [double pointer ++i, ++j]](/img/8c/cc5361caefceb6a4eb1c2ef8d5dede.png)
455. Distribute cookies [double pointer ++i, ++j]

ALV屏幕输入选项学习
随机推荐
How to design test cases well
The United States, Japan and South Korea jointly developed 6G with the intention of anti surpassing, but China has long been prepared
此章节用于补充3
Tensor Rt的int8量化原理
Netease game R & D Engineer Intern (client side)
LeetCode_ 134_ gas station
简述MES系统的11大核心功能模块
Operation: skillfully use MySQL master-slave replication delay to save erroneously deleted data
工赋开发者社区 | 定了!就在7月30日!
数据库索引的原理,为什么要用 B+树,为什么不用二叉树?
[interview question] 1384- share 44 JS problems. Half right is a master
IrrKlang音频库的下载和配置
2022年云商店联合营销市场发展基金(MDF)介绍
测试组如何进行QA规范
接口测试方案(接口测试思路)
Linked list - reverse linked list
自动化测试工具-Playwright(快速上手)
SSM integration - exception handler and project exception handling scheme
Understand in depth why not use system.out.println()
探索式软件测试


