当前位置:网站首页>模板进阶(跑路人笔记)
模板进阶(跑路人笔记)
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;
}
};
边栏推荐
- SSM整合-功能模块和接口测试
- Some tips for printing logs
- J9 number theory: how to avoid the trap of stepping on thunder?
- Mpc5744p burning to 98% can not continue to download the program
- Still using xshell? Recommend this more modern terminal connection tool
- 多商户商城系统功能拆解16讲-平台端会员成长值记录
- 一文详解MES系统给企业带来的5大好处,附应用场景
- Redis core principles
- 更安全、更健康、无续航焦虑,魏牌拿铁DHT-PHEV来了
- The third day of SSM practice_ Paging assistant_ Security framework
猜你喜欢

Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here

MySQL exercises elementary 45 questions (Unified table)

MySQL 遇到过死锁问题吗,你是如何解决的?

If the recommendation effect is not satisfactory, it's better to try to learn the propeller chart

SSM integration - functional module and interface testing

MPLS experiment

Sentinel 隔离与降级

图解用户登录验证流程,写得太好了!

FTP协议

Seata 入门简介
随机推荐
Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here
The third day of SSM practice_ Paging assistant_ Security framework
云服务器mySQL提示报错
SSM integration configuration
工赋开发者社区 | 定了!就在7月30日!
Multi merchant mall system function disassembly Lecture 16 - platform side member growth value record
SSM整合-异常处理器和项目异常处理方案
Concentrate, heart to heart! The Chinese funded mobile phone Enterprises Association (CMA) of India is officially operational!
LeetCode_1005_K次取反后最大化的数组和
开发winform中遇到的一些问题汇总(持续跟新)
455. Distribute cookies [double pointer ++i, ++j]
MySQL练习题初级45题(统一表)
SSM整-整合配置
测试组如何进行QA规范
LeetCode_ 1005_ Maximized array sum after K negations
Summary of some problems encountered in developing WinForm (continuous updating)
OpenGL中的视差贴图的着色器代码
骚操作:巧用MySQL主从复制延迟拯救误删数据
这场竞赛,能读懂题目的你大有可为
Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station


