news 2026/2/11 0:22:37

【c++】 模板初阶

作者头像

张小明

前端开发工程师

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

泛型编程

写一个交换函数,在学习模板之前,为了匹配不同的参数类型,我们可以利用函数重载来实现。

代码语言:javascript

AI代码解释

void Swap(int& a, int& b) { int c = a; a = b; b = c; } void Swap(char& a, char& b) { char c = a; a = b; b = c; } void Swap(double& a, double& b) { double c = a; a = b; b = c; } //...

虽然这样似乎解决了问题,但是这样的设计写着太过麻烦,只要出现新类型就需要写新的函数,代码的复用率很低。有没有什么可以让我们一劳永逸呢?模板就可以实现这一功能。

这种通过抽象和模板化来编写可重用和灵活的代码以此提升代码的可读性和维护性,同时避免代码重复的方式称为泛型编程。

函数模板

函数模板是c++中的一类机制,通过在函数定义中使用模板参数,我们可以编写一个函数,而在调用时根据实际参数的类型自动生成相应的版本。

代码语言:javascript

AI代码解释

template <class T> void Swap(T& a, T& b) { T c = a; a = b; b = c; }

这样编译器就可以根据传入的参数类型来生成对应的Swap()函数,大大提高了代码的复用率。下面我们来尝试运行一下。

代码语言:javascript

AI代码解释

template <class T> void Swap(T& a, T& b) { T c = a; a = b; b = c; } int main() { int a,b; a = 1; b = 2; double c, d; c = 0.0; d = 1.2; Swap(a, b); Swap(c, d); cout << a << " " << b << endl; cout << c << " " << d << endl; return 0; }

我们发现,调用Swap()之后,int类型的ab和double类型的cd都完成了交换。但是他们是否调的是同一个函数呢?

转到反汇编:

我们发现两次调用的是不同的Swap()函数,根据传入参数类型的不同 ,编译器会生成不同的函数。然后再调用生成的函数。

函数模板的实例化

通过函数模板生成对应函数的过程叫做函数实例化。

当模板的参数只有一个时,却传入了不同类型的变量,编译器无法推导出T的类型,出现了推导错误

www.dongchedi.com/article/7594239818788389438
www.dongchedi.com/article/7594239354315817496
www.dongchedi.com/article/7594242086645187097
www.dongchedi.com/article/7594242305499382296
www.dongchedi.com/article/7594241690249167385
www.dongchedi.com/article/7594243296965411352
www.dongchedi.com/article/7594244056763695678
www.dongchedi.com/article/7594243281903649342


www.dongchedi.com/article/7594176187270693401
www.dongchedi.com/article/7594176654025589273
www.dongchedi.com/article/7594174513613636120
www.dongchedi.com/article/7594173614161723966
www.dongchedi.com/article/7594174316762726937
www.dongchedi.com/article/7594174486359114264
www.dongchedi.com/article/7594172209028399641
www.dongchedi.com/article/7594172028136112665
www.dongchedi.com/article/7594170708973158937
www.dongchedi.com/article/7594171822011318809
www.dongchedi.com/article/7594169237096940057
www.dongchedi.com/article/7594169267902857753
www.dongchedi.com/article/7594154183356727870
www.dongchedi.com/article/7594153895329448472
www.dongchedi.com/article/7594153335339647550
www.dongchedi.com/article/7594149462356804121
www.dongchedi.com/article/7594143242124427801
www.dongchedi.com/article/7594143151032959513
www.dongchedi.com/article/7594143310076477976
www.dongchedi.com/article/7594143261762585112
www.dongchedi.com/article/7594143234042380824
www.dongchedi.com/article/7594143047861420569
www.dongchedi.com/article/7594143261762257432
www.dongchedi.com/article/7594113295314305561
www.dongchedi.com/article/7594111799855956505
www.dongchedi.com/article/7594110342540673598
www.dongchedi.com/article/7594110249179578904
www.dongchedi.com/article/7594109373581492761
www.dongchedi.com/article/7594199698655724056
www.dongchedi.com/article/7594197415100793369
www.dongchedi.com/article/7594197322150904382
www.dongchedi.com/article/7594196529834181145
www.dongchedi.com/article/7594196856478368281
www.dongchedi.com/article/7594195826612781592
www.dongchedi.com/article/7594195431069762072
www.dongchedi.com/article/7594195431069270552
www.dongchedi.com/article/7594179258625802776
www.dongchedi.com/article/7594177577032991257
www.dongchedi.com/article/7594179258625409560
www.dongchedi.com/article/7594177972484309529
www.dongchedi.com/article/7594176579102884414
www.dongchedi.com/article/7594177062877839897
www.dongchedi.com/article/7594174721596473881
www.dongchedi.com/article/7594175192449008153
www.dongchedi.com/article/7594173652875444760
www.dongchedi.com/article/7594172550394069566
www.dongchedi.com/article/7594172678253265470
www.dongchedi.com/article/7594171407253275198
www.dongchedi.com/article/7594169982704173593
www.dongchedi.com/article/7594171805393650201
www.dongchedi.com/article/7594171076024812094
www.dongchedi.com/article/7594169163703271998
www.dongchedi.com/article/7594155915663180313
www.dongchedi.com/article/7594152900747100734
www.dongchedi.com/article/7594151966180426264
www.dongchedi.com/article/7594148426036904473
www.dongchedi.com/article/7594143100311192088

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

浙江大学LaTeX论文模板:告别格式困扰的终极解决方案

浙江大学LaTeX论文模板&#xff1a;告别格式困扰的终极解决方案 【免费下载链接】zjuthesis Zhejiang University Graduation Thesis LaTeX Template 项目地址: https://gitcode.com/gh_mirrors/zj/zjuthesis 还在为论文格式调整而烦恼吗&#xff1f;面对学校严格的排版…

作者头像 李华
网站建设 2026/2/3 3:18:40

CircuitJS1 Desktop Mod:离线电路模拟的终极解决方案

CircuitJS1 Desktop Mod&#xff1a;离线电路模拟的终极解决方案 【免费下载链接】circuitjs1 Standalone (offline) version of the Circuit Simulator based on NW.js. 项目地址: https://gitcode.com/gh_mirrors/circ/circuitjs1 在电子工程学习和实践的道路上&#…

作者头像 李华
网站建设 2026/2/6 16:13:28

D触发器电路图设计要点:以74HC74为核心的一文说清

从零开始搞懂D触发器设计&#xff1a;74HC74实战全解析 你有没有遇到过这样的情况&#xff1f; 明明逻辑写得没问题&#xff0c;时序仿真也通过了&#xff0c;可一上电&#xff0c;Q输出就是乱跳&#xff1b;或者系统每次上电状态都不一样&#xff0c;像是“中了邪”。 别急—…

作者头像 李华
网站建设 2026/2/4 9:30:04

手机秒变专业直播摄像头:DroidCam OBS插件深度应用指南

手机秒变专业直播摄像头&#xff1a;DroidCam OBS插件深度应用指南 【免费下载链接】droidcam-obs-plugin DroidCam OBS Source 项目地址: https://gitcode.com/gh_mirrors/dr/droidcam-obs-plugin 还在为直播设备预算发愁&#xff1f;DroidCam OBS插件为你打开全新的创…

作者头像 李华
网站建设 2026/2/5 18:30:30

Rusted PackFile Manager:Total War MOD开发的革命性效率工具

Rusted PackFile Manager&#xff1a;Total War MOD开发的革命性效率工具 【免费下载链接】rpfm Rusted PackFile Manager (RPFM) is a... reimplementation in Rust and Qt5 of PackFile Manager (PFM), one of the best modding tools for Total War Games. 项目地址: http…

作者头像 李华
网站建设 2026/2/3 9:44:47

Mac完美读写NTFS硬盘:Free-NTFS-for-Mac开源工具全面体验

Mac完美读写NTFS硬盘&#xff1a;Free-NTFS-for-Mac开源工具全面体验 【免费下载链接】Free-NTFS-for-Mac Nigate&#xff0c;一款支持苹果芯片的Free NTFS for Mac小工具软件。NTFS R/W for macOS. Support Intel/Apple Silicon now. 项目地址: https://gitcode.com/gh_mirr…

作者头像 李华