news 2026/5/1 10:37:40

【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/5/1 8:01:38

SmartDock桌面启动器:5步打造专属Android工作站

SmartDock桌面启动器&#xff1a;5步打造专属Android工作站 【免费下载链接】smartdock A user-friendly desktop mode launcher that offers a modern and customizable user interface 项目地址: https://gitcode.com/gh_mirrors/smar/smartdock SmartDock是一款专为A…

作者头像 李华
网站建设 2026/5/1 7:57:38

StructBERT零样本分类器性能对比:不同文本长度的表现

StructBERT零样本分类器性能对比&#xff1a;不同文本长度的表现 1. 引言&#xff1a;AI 万能分类器的兴起与挑战 在自然语言处理&#xff08;NLP&#xff09;领域&#xff0c;文本分类是构建智能系统的核心能力之一。传统方法依赖大量标注数据进行监督训练&#xff0c;成本高…

作者头像 李华
网站建设 2026/5/1 7:57:43

E-Hentai漫画下载终极指南:一键批量收藏完整教程

E-Hentai漫画下载终极指南&#xff1a;一键批量收藏完整教程 【免费下载链接】E-Hentai-Downloader Download E-Hentai archive as zip file 项目地址: https://gitcode.com/gh_mirrors/eh/E-Hentai-Downloader 还在为E-Hentai上心爱的漫画无法批量下载而烦恼吗&#xf…

作者头像 李华
网站建设 2026/5/1 9:13:04

BetterJoy控制器完整配置方案:4种场景的终极解决方案

BetterJoy控制器完整配置方案&#xff1a;4种场景的终极解决方案 【免费下载链接】BetterJoy Allows the Nintendo Switch Pro Controller, Joycons and SNES controller to be used with CEMU, Citra, Dolphin, Yuzu and as generic XInput 项目地址: https://gitcode.com/g…

作者头像 李华
网站建设 2026/5/1 9:08:49

AI万能分类器应用实战:构建智能问答系统

AI万能分类器应用实战&#xff1a;构建智能问答系统 1. 引言&#xff1a;AI 万能分类器的现实价值 在当今信息爆炸的时代&#xff0c;企业每天需要处理海量的用户反馈、客服对话、工单请求和社交媒体评论。如何从这些非结构化文本中快速提取意图、自动归类并做出响应&#xf…

作者头像 李华
网站建设 2026/5/1 9:14:56

wechat-need-web:告别微信网页版访问限制的技术利器

wechat-need-web&#xff1a;告别微信网页版访问限制的技术利器 【免费下载链接】wechat-need-web 让微信网页版可用 / Allow the use of WeChat via webpage access 项目地址: https://gitcode.com/gh_mirrors/we/wechat-need-web 还在为微信网页版频繁出现的"请在…

作者头像 李华