material/c++/universal/template.md
2025-01-10 22:29:51 +08:00

44 lines
736 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 模板
## 函数模板
### 基本格式
```cpp
template <typename T>
void swap(T &a, T &b) {
T temp;
temp = b;
b = a;
a = temp;
}
```
__函数模板必须放在头文件里因为它不是函数实体只能算编译指令__
### 模板的几个术语
+ **隐式实例化**
```cpp
int a=0, b=3;
swap<int>(a, b);
```
+ **显式实例化**
```cpp
template void swap<int>(int&, int&); // 使用模板生成int类型的函数定义
```
+ **显式具体化**
```cpp
template <> void swap<int>(int&, int&);
```
__这个可以放到其他文件里因为是有实体的__
### 类型推导
+ decltype()
```cpp
int a;
decltype(a) var;
```
## 类模板
+ 同样没有实体,包括类成员方法等都要写成模板的格式