泛型编程
写一个交换函数,在学习模板之前,为了匹配不同的参数类型,我们可以利用函数重载来实现。
代码语言: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