news 2026/6/10 18:26:56

【c++】模板进阶

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【c++】模板进阶

在一些特定的情况,会用到常量;如我们需要一个定长的数组时。控制数组长度的类型时确定的(如size_t),这样我们只设置一个类型参数就可以了。

代码语言:javascript

AI代码解释

namespace xian { //定义一个模板类型的定长数组 template<typename T,size_t N> class array { public: T& operator[](size_t n) { return _array[n]; } const T& operator[](size_t n) const { return _array[n]; } size_t size() { return _size; } bool empty() { return 0 == _size; } private: T _array[N]; size_t _size; }; }

注意:非类型模板参数只能用于整型;不支持浮点数,类对象和字符串。c++20之后可以支持double作非类型模板参数。

非类型模板参数支持缺省值:

代码语言:javascript

AI代码解释

template<size_t N = 10> class Stack { private: int a[N]; int _top; }; int main() { //Stack s0; 这种写法要c++20以后才支持 Stack<> s1; //我们还是用这种写法的好 }

模板的特化

函数模板的特化

代码语言:javascript

AI代码解释

// 函数模板 -- 参数匹配 template<class T> bool Less(T left, T right) { return left < right; } int main() { cout << Less(1, 2) << endl; Date d2(2022, 7, 8); Date d1(2022, 7, 7); cout << Less(d1, d2) << endl;//可以正常判断 Date* p2 = &d2; Date* p1 = &d1; cout << Less(p1, p2) << endl; //会直接比较指针 return 0; }

根据地址比较大小,而地址的申请时随机的,这样结果是不能确定的。对于这种有特殊情况的可以使用模板的特化,把特殊情况单独拿出来处理。

代码语言:javascript

AI代码解释

// 函数模板 -- 参数匹配 template<class T> bool Less(T left, T right) { return left < right; } // 对Less函数模板进行特化 template<> bool Less<Date*>(Date* left, Date* right) { return *left < *right; } int main() { cout << Less(1, 2) << endl; Date d2(2022, 7, 8); Date d1(2022, 7, 7); cout << Less(d1, d2) << endl;//可以正常判断 Date* p2 = &d2; Date* p1 = &d1; cout << Less(p1, p2) << endl; //会直接比较指针 return 0; }

有特化后的类之后,会直接使用特化后的类。

总结:

  • 主模板:定义通用的函数模板。
  • 特化版本:为特定类型提供定制化实现。
  • 调用:编译器根据参数类型自动选择主模板或特化版本。
  • 注意事项:特化版本的函数签名必须与主模板一致,且不支持偏特化。

大多数时候,我们都选择简单的方法直接把特殊的情况函数直接给出而不是使用特化。使用特化容易出错,一般不要使用函数模板特化

https://www.dongchedi.com/article/7594153635873882649
https://www.dongchedi.com/article/7594154845427827225
https://www.dongchedi.com/article/7594151436733514264
https://www.dongchedi.com/article/7594148355094708761
https://www.dongchedi.com/article/7594143059890602558
https://www.dongchedi.com/article/7594142952818426392
https://www.dongchedi.com/article/7594142964432323096
https://www.dongchedi.com/article/7594143059890209342
https://www.dongchedi.com/article/7594143001568707134
https://www.dongchedi.com/article/7594142838871376409
https://www.dongchedi.com/article/7594113284840669758
https://www.dongchedi.com/article/7594110260978238014
https://www.dongchedi.com/article/7594110220667028030
https://www.dongchedi.com/article/7594109748606665278
https://www.dongchedi.com/article/7594110099447513624
https://www.dongchedi.com/article/7594108451488334398
https://www.dongchedi.com/article/7594106177244561945
https://www.dongchedi.com/article/7594103370105979417
https://www.dongchedi.com/article/7594199071733596734
https://www.dongchedi.com/article/7594198145211761176
https://www.dongchedi.com/article/7594196964511154713
https://www.dongchedi.com/article/7594198239285494297
https://www.dongchedi.com/article/7594197322151068222
https://www.dongchedi.com/article/7594198145211925016
https://www.dongchedi.com/article/7594195638192243225
https://www.dongchedi.com/article/7594198041578455614
https://www.dongchedi.com/article/7594179233010893336
https://www.dongchedi.com/article/7594179607033922110
https://www.dongchedi.com/article/7594177906826625560
https://www.dongchedi.com/article/7594177530207289918
https://www.dongchedi.com/article/7594176033486864958
https://www.dongchedi.com/article/7594174514142462488
https://www.dongchedi.com/article/7594175199944213017
https://www.dongchedi.com/article/7594173003894964798
https://www.dongchedi.com/article/7594172411499954713
https://www.dongchedi.com/article/7594172274706514456
https://www.dongchedi.com/article/7594171609930383897
https://www.dongchedi.com/article/7594171501943931416
https://www.dongchedi.com/article/7594171895055286808
https://www.dongchedi.com/article/7594169899916853785
https://www.dongchedi.com/article/7594168856931762712
https://www.dongchedi.com/article/7594154845427630617
https://www.dongchedi.com/article/7594152696526504510
https://www.dongchedi.com/article/7594154096370991678
https://www.dongchedi.com/article/7594149478186533401
https://www.dongchedi.com/article/7594143476796015128
https://www.dongchedi.com/article/7594143420428370457
https://www.dongchedi.com/article/7594143309313147417
https://www.dongchedi.com/article/7594143309313081881
https://www.dongchedi.com/article/7594143555656942104
https://www.dongchedi.com/article/7594143559859765822
https://www.dongchedi.com/article/7594112718668349977
https://www.dongchedi.com/article/7594111610630013464
https://www.dongchedi.com/article/7594112414933795353
https://www.dongchedi.com/article/7594110167684661784
https://www.dongchedi.com/article/7594108789133951550
https://www.dongchedi.com/article/7594110242670101016
https://www.dongchedi.com/article/7594108398888010302
https://www.dongchedi.com/article/7594198471734198809
https://www.dongchedi.com/article/7594199227367309886
https://www.dongchedi.com/article/7594196987835466264
https://www.dongchedi.com/article/7594196535337353752
https://www.dongchedi.com/article/7594197773559185944
https://www.dongchedi.com/article/7594196903693156888
https://www.dongchedi.com/article/7594197731099771416
https://www.dongchedi.com/article/7594196461773406745
https://www.dongchedi.com/article/7594180373895709246
https://www.dongchedi.com/article/7594178432767017497
https://www.dongchedi.com/article/7594177510213272088
https://www.dongchedi.com/article/7594177633563460121

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 15:54:08

PCL2-CE社区版启动器:打造你的专属Minecraft游戏管家

PCL2-CE社区版启动器&#xff1a;打造你的专属Minecraft游戏管家 【免费下载链接】PCL2-CE PCL2 社区版&#xff0c;可体验上游暂未合并的功能 项目地址: https://gitcode.com/gh_mirrors/pc/PCL2-CE 还在为繁琐的Minecraft启动流程而烦恼吗&#xff1f;想要一款既稳定又…

作者头像 李华
网站建设 2026/6/10 16:04:37

Minecraft NBT编辑器终极探索:从数据新手到修改大师的实战秘籍

Minecraft NBT编辑器终极探索&#xff1a;从数据新手到修改大师的实战秘籍 【免费下载链接】NBTExplorer A graphical NBT editor for all Minecraft NBT data sources 项目地址: https://gitcode.com/gh_mirrors/nb/NBTExplorer 你是否曾经因为Minecraft存档损坏而痛心…

作者头像 李华
网站建设 2026/6/10 16:04:44

新手必看:高速PCB仿真前处理操作指南

高速PCB仿真前处理&#xff1a;新手避坑指南与实战精要你有没有遇到过这样的情况&#xff1f;辛辛苦苦做完高速板子的布局布线&#xff0c;信心满满地跑完信号完整性&#xff08;SI&#xff09;仿真&#xff0c;结果眼图闭合、串扰超标。回头改了几处走线&#xff0c;问题依旧。…

作者头像 李华
网站建设 2026/6/9 23:49:38

碧蓝航线Alas:终极自动化管理完整指南

碧蓝航线Alas&#xff1a;终极自动化管理完整指南 【免费下载链接】AzurLaneAutoScript Azur Lane bot (CN/EN/JP/TW) 碧蓝航线脚本 | 无缝委托科研&#xff0c;全自动大世界 项目地址: https://gitcode.com/gh_mirrors/az/AzurLaneAutoScript 还在为重复的舰队管理任务…

作者头像 李华
网站建设 2026/6/10 17:05:37

ResNet18部署案例:智能工厂监控系统

ResNet18部署案例&#xff1a;智能工厂监控系统 1. 引言&#xff1a;通用物体识别在工业场景中的价值 随着智能制造的快速发展&#xff0c;智能工厂对实时、精准的视觉感知能力提出了更高要求。传统监控系统仅能实现“录像回溯”&#xff0c;而无法主动理解画面内容。引入基于…

作者头像 李华
网站建设 2026/6/9 21:32:56

深蓝词库转换终极指南:5步搞定20+输入法词库无缝迁移

深蓝词库转换终极指南&#xff1a;5步搞定20输入法词库无缝迁移 【免费下载链接】imewlconverter ”深蓝词库转换“ 一款开源免费的输入法词库转换程序 项目地址: https://gitcode.com/gh_mirrors/im/imewlconverter 还在为换输入法就要重新积累词库而烦恼吗&#xff1f…

作者头像 李华