一、非类型模板参数
1. 认识
模板参数分为类类型形参和非类型形参。
- 类类型模板参数:出现在模板参数列表中,跟在class或者typename之类的参数类型名称。
- 非类型模板参数:就是用一个常量作为类(函数)模板的一个参数,在类(函数)模板中可将该参数当成常量来使用。
2. 使用
例如,我们要实现一个静态数组的类,就需要用到非类型模板参数。
代码语言:javascript
AI代码解释
template<class T, size_t N> //N:非类型模板参数 class StaticArray { public: size_t arraysize() { return N; } private: T _array[N]; //利用非类型模板参数指定静态数组的大小 }; int main() { StaticArray<int, 10> a1; //定义一个大小为10的静态数组 cout << a1.arraysize() << endl; //10 StaticArray<int, 100> a2; //定义一个大小为100的静态数组 cout << a2.arraysize() << endl; //100 return 0; }3. 和类类型参数的对比
特点 | 类型模板参数 (Type Template Parameter) | 非类型模板参数 (Non-type Template Parameter) |
|---|---|---|
参数形式 | 类型(如 int, double, std::string) | 编译期常量值(如整数、枚举、指针、引用、C++20起支持浮点和字面量类) |
传参方式 | Array<int> | Array<int, 10> |
常见用途 | 泛型编程、容器、算法 | 固定大小容器、编译期策略选择、哈希参数等 |
编译期要求 | 只要类型合法即可 | 必须是编译期常量 |
C++17 支持范围 | 所有类型 | 整数、枚举、指针/引用常量 |
C++20 新增支持 | - | 浮点数、字面量类类型(满足 constexpr) |
举例 | template <typename T> class Vec {} | template <typename T, int N> class Array {} |
二、arry容器
1. 认识
在这里插入图片描述
在这里插入图片描述
我们可以看出arry容器也使用的非类型模板参数来管理数组的固定大小。
2. 使用
代码语言:javascript
AI代码解释
#include<iostream> #include<array> using namespace std; int main() { //array容器的使用 array<int, 8> dh; //定义一个大小为8的静态数组 dh[0] = 6; //第一个数据初始化为6 dh[7] = 99; //最后一个数据初始化为99 for (auto e : dh) //输出dh数组 { cout << e << ' '; } cout << endl; return 0; }在这里插入图片描述
我们发现对未进行赋值的数据并没有对其进行初始化,反而是给出的随机值,那他和我们c语言中的传统数组有什么不同呢?又有什么优势?
3. 和传统c语言数组的对比
代码语言:javascript
AI代码解释
#include<iostream> #include<array> using namespace std; int main() { //array容器的使用 array<int, 8> dh; //定义一个大小为8的静态数组 dh[0] = 6; //第一个数据初始化为6 dh[7] = 99; //最后一个数据初始化为99 for (auto e : dh) //输出dh数组 { cout << e << ' '; } cout << endl; //c语言传统数组 int arr[8]; arr[0] = 6; arr[7] = 99; for (auto e : arr) //输出dh数组 { cout << e << ' '; } cout << endl; return 0; }在这里插入图片描述
同样的,都没有进行初始化,而且c语言中的数组也支持array中的大部分数组,其实其中的不同就是array容器对越界的检查更加的严格,可以检查出越界读和越界写。
看下面的两个错误写法:
在这里插入图片描述
在这里插入图片描述
但是c语言中也可以检查出部分越界写
在这里插入图片描述
三、模板特化
1. 概念
我们通过一个代码的例子来理解模板的特化
代码语言:javascript
AI代码解释
#include<iostream> using namespace std; // 函数模板 -- 参数匹配 template<class T> bool IsEqual(T x, T y) //判断是否相等 { return x == y; } int main() { cout << IsEqual(1, 1) << endl; //1 cout << IsEqual(1.1, 2.2) << endl; //0 char a1[] = "2021bsdt"; char a2[] = "2021bsdt"; cout << IsEqual(a1, a2) << endl; //0 return 0; }在这里插入图片描述
判断结果是这两个字符串不相等,这很好理解,因为我们希望的是该函数能够判断两个字符串的内容是否相等,而该函数实际上判断是确实这两个字符串所存储的地址是否相同,这是两个存在于栈区的字符串,其地址显然是不同的。类似于上述实例,使用模板可以实现一些与类型无关的代码,但对于一些特殊的类型可能会得到一些错误的结果,此时就需要对模板进行特化,即在原模板的基础上,针对特殊类型进行特殊化的实现方式
2. 函数模板特化
函数模板的特化步骤:
- 必须要先有一个基础的函数模板
- 关键字template后面接一对空的尖括号<>
- 函数名后跟一对尖括号,尖括号中指定需要特化的类型
- 函数形参表: 必须要和模板函数的基础参数类型完全相同,如果不同编译器可能会报一些奇怪的错误
我们知道当传入的类型是char时,应该依次比较各个字符的ASCII码值进而判断两个字符串是否相等,或是直接调用strcmp函数进行字符串比较,那么此时我们就可以对char类型进行特殊化的实现。
m.xnnbb.pro/post/11757.html
m.xnnbb.pro/post/79719.html
m.xnnbb.pro/post/24682.html
m.xnnbb.pro/post/68046.html
m.xnnbb.pro/post/44202.html
m.xnnbb.pro/post/33995.html
m.xnnbb.pro/post/84266.html
m.xnnbb.pro/post/35555.html
m.xnnbb.pro/post/46260.html
m.xnnbb.pro/post/57197.html
m.xnnbb.pro/post/35133.html
m.xnnbb.pro/post/62442.html
m.xnnbb.pro/post/57593.html
m.xnnbb.pro/post/51913.html
m.xnnbb.pro/post/75575.html
m.xnnbb.pro/post/75777.html
m.xnnbb.pro/post/93391.html
m.xnnbb.pro/post/35575.html
m.xnnbb.pro/post/57393.html
m.xnnbb.pro/post/91717.html
m.xnnbb.pro/post/37917.html
m.xnnbb.pro/post/39713.html
m.xnnbb.pro/post/19999.html
m.xnnbb.pro/post/13943.html
m.xnnbb.pro/post/17797.html
m.xnnbb.pro/post/57117.html
m.xnnbb.pro/post/53777.html
m.xnnbb.pro/post/19555.html
m.xnnbb.pro/post/97931.html
m.xnnbb.pro/post/17917.html
m.xnnbb.pro/post/59995.html
m.xnnbb.pro/post/26004.html
m.xnnbb.pro/post/33979.html
m.xnnbb.pro/post/79795.html
m.xnnbb.pro/post/19337.html
m.xnnbb.pro/post/77371.html
m.xnnbb.pro/post/13599.html
m.xnnbb.pro/post/99553.html
m.xnnbb.pro/post/99511.html
m.xnnbb.pro/post/80848.html
m.xnnbb.pro/post/19733.html
m.xnnbb.pro/post/19711.html
m.xnnbb.pro/post/33771.html
m.xnnbb.pro/post/44668.html
m.xnnbb.pro/post/15399.html
m.xnnbb.pro/post/13491.html
m.xnnbb.pro/post/93571.html
m.xnnbb.pro/post/53177.html
m.xnnbb.pro/post/11317.html
m.xnnbb.pro/post/46046.html
m.xnnbb.pro/post/68620.html
m.xnnbb.pro/post/97313.html
m.xnnbb.pro/post/19777.html
m.xnnbb.pro/post/68466.html
m.xnnbb.pro/post/37777.html
m.xnnbb.pro/post/02280.html
m.xnnbb.pro/post/99339.html
m.xnnbb.pro/post/97377.html
m.xnnbb.pro/post/57337.html
m.xnnbb.pro/post/73913.html
m.xnnbb.pro/post/73593.html
m.xnnbb.pro/post/15335.html
m.xnnbb.pro/post/95377.html
m.xnnbb.pro/post/13795.html
m.xnnbb.pro/post/91979.html
m.xnnbb.pro/post/75913.html
m.xnnbb.pro/post/33975.html
m.xnnbb.pro/post/19779.html
m.xnnbb.pro/post/31513.html
m.xnnbb.pro/post/73931.html
m.xnnbb.pro/post/17757.html
m.xnnbb.pro/post/91519.html
m.xnnbb.pro/post/97133.html
m.xnnbb.pro/post/68808.html
m.xnnbb.pro/post/68448.html
m.xnnbb.pro/post/79953.html
m.xnnbb.pro/post/97191.html
m.xnnbb.pro/post/51199.html
m.xnnbb.pro/post/44002.html
m.xnnbb.pro/post/17513.html
m.xnnbb.pro/post/15359.html
m.xnnbb.pro/post/48444.html
m.xnnbb.pro/post/55113.html
m.xnnbb.pro/post/15319.html
m.xnnbb.pro/post/37173.html
m.xnnbb.pro/post/86240.html
m.xnnbb.pro/post/77775.html
m.xnnbb.pro/post/39511.html
m.xnnbb.pro/post/97991.html
m.xnnbb.pro/post/68886.html
m.xnnbb.pro/post/53933.html
m.xnnbb.pro/post/53191.html
m.xnnbb.pro/post/51333.html
m.xnnbb.pro/post/03059.html
m.xnnbb.pro/post/53751.html
m.xnnbb.pro/post/53711.html
m.xnnbb.pro/post/39933.html
m.xnnbb.pro/post/22428.html
m.xnnbb.pro/post/71935.html
m.xnnbb.pro/post/31731.html
m.xnnbb.pro/post/53715.html
m.xnnbb.pro/post/04680.html
m.xnnbb.pro/post/84680.html
m.xnnbb.pro/post/95751.html
m.xnnbb.pro/post/55575.html
m.xnnbb.pro/post/60686.html
m.xnnbb.pro/post/71311.html
m.xnnbb.pro/post/59115.html
m.xnnbb.pro/post/77517.html
m.xnnbb.pro/post/72178.html
m.xnnbb.pro/post/79591.html
m.xnnbb.pro/post/53353.html
m.xnnbb.pro/post/95715.html
m.xnnbb.pro/post/95315.html
m.xnnbb.pro/post/24466.html
m.xnnbb.pro/post/13553.html
m.xnnbb.pro/post/91917.html
m.xnnbb.pro/post/20820.html
m.xnnbb.pro/post/08260.html
m.xnnbb.pro/post/13535.html
m.xnnbb.pro/post/91359.html
m.xnnbb.pro/post/08466.html
m.xnnbb.pro/post/55199.html
m.xnnbb.pro/post/75957.html
m.xnnbb.pro/post/33511.html
m.xnnbb.pro/post/71837.html
m.xnnbb.pro/post/99753.html
m.xnnbb.pro/post/39173.html
m.xnnbb.pro/post/55759.html
m.xnnbb.pro/post/31135.html
m.xnnbb.pro/post/91793.html
m.xnnbb.pro/post/35155.html
m.xnnbb.pro/post/75557.html
m.xnnbb.pro/post/77539.html
m.xnnbb.pro/post/15131.html
m.xnnbb.pro/post/59351.html
m.xnnbb.pro/post/86660.html
m.xnnbb.pro/post/20080.html
m.xnnbb.pro/post/37711.html
m.xnnbb.pro/post/93935.html
m.xnnbb.pro/post/31933.html
m.xnnbb.pro/post/19939.html
m.xnnbb.pro/post/48020.html
m.xnnbb.pro/post/79739.html
m.xnnbb.pro/post/77977.html
m.xnnbb.pro/post/55715.html
m.xnnbb.pro/post/51171.html
m.xnnbb.pro/post/15959.html
m.xnnbb.pro/post/99539.html
m.xnnbb.pro/post/84686.html
m.xnnbb.pro/post/13717.html
m.xnnbb.pro/post/42020.html
m.xnnbb.pro/post/20004.html
m.xnnbb.pro/post/75159.html
m.xnnbb.pro/post/57199.html
m.xnnbb.pro/post/59975.html
m.xnnbb.pro/post/00288.html
m.xnnbb.pro/post/95793.html
m.xnnbb.pro/post/35779.html
m.xnnbb.pro/post/42820.html
m.xnnbb.pro/post/57195.html
m.xnnbb.pro/post/79597.html
m.xnnbb.pro/post/31357.html
m.xnnbb.pro/post/59399.html
m.xnnbb.pro/post/55719.html
m.xnnbb.pro/post/93313.html
m.xnnbb.pro/post/88280.html
m.xnnbb.pro/post/99155.html
m.xnnbb.pro/post/48408.html
m.xnnbb.pro/post/19359.html
m.xnnbb.pro/post/15595.html
m.xnnbb.pro/post/66246.html
m.xnnbb.pro/post/11955.html
m.xnnbb.pro/post/55917.html
m.xnnbb.pro/post/91559.html
m.xnnbb.pro/post/26280.html
m.xnnbb.pro/post/39571.html
m.xnnbb.pro/post/79175.html
m.xnnbb.pro/post/17157.html
m.xnnbb.pro/post/97975.html
m.xnnbb.pro/post/57951.html
m.xnnbb.pro/post/73173.html
m.xnnbb.pro/post/59111.html
m.xnnbb.pro/post/75513.html
m.xnnbb.pro/post/37577.html
m.xnnbb.pro/post/31537.html
m.xnnbb.pro/post/84286.html
m.xnnbb.pro/post/39931.html
m.xnnbb.pro/post/93373.html
m.xnnbb.pro/post/60664.html
m.xnnbb.pro/post/20222.html
m.xnnbb.pro/post/75531.html
m.xnnbb.pro/post/55979.html
m.xnnbb.pro/post/57793.html
m.xnnbb.pro/post/39913.html
m.xnnbb.pro/post/91113.html
m.xnnbb.pro/post/19931.html
m.xnnbb.pro/post/19973.html
m.xnnbb.pro/post/31579.html
m.xnnbb.pro/post/86226.html
m.xnnbb.pro/post/77959.html
m.xnnbb.pro/post/82068.html
m.xnnbb.pro/post/55557.html
m.xnnbb.pro/post/71795.html
m.xnnbb.pro/post/75771.html
m.xnnbb.pro/post/35591.html
m.xnnbb.pro/post/95755.html
m.xnnbb.pro/post/73139.html
m.xnnbb.pro/post/19731.html
m.xnnbb.pro/post/57157.html
m.xnnbb.pro/post/84048.html
m.xnnbb.pro/post/19519.html
m.xnnbb.pro/post/79599.html
m.xnnbb.pro/post/79771.html
m.xnnbb.pro/post/73915.html
m.xnnbb.pro/post/11359.html
m.xnnbb.pro/post/42680.html
m.xnnbb.pro/post/93377.html
m.xnnbb.pro/post/64044.html
m.xnnbb.pro/post/95579.html
m.xnnbb.pro/post/31193.html
m.xnnbb.pro/post/97353.html
m.xnnbb.pro/post/15915.html
m.xnnbb.pro/post/93537.html
m.xnnbb.pro/post/55173.html
m.xnnbb.pro/post/13591.html
m.xnnbb.pro/post/31531.html
m.xnnbb.pro/post/60020.html
m.xnnbb.pro/post/28246.html
m.xnnbb.pro/post/13159.html
m.xnnbb.pro/post/17791.html
m.xnnbb.pro/post/37515.html
m.xnnbb.pro/post/33333.html
m.xnnbb.pro/post/31711.html
m.xnnbb.pro/post/77397.html
m.xnnbb.pro/post/71595.html
m.xnnbb.pro/post/91571.html
m.xnnbb.pro/post/13517.html
m.xnnbb.pro/post/15159.html
m.xnnbb.pro/post/42082.html
m.xnnbb.pro/post/13797.html
m.xnnbb.pro/post/39515.html
m.xnnbb.pro/post/66820.html
m.xnnbb.pro/post/28080.html
m.xnnbb.pro/post/53791.html
m.xnnbb.pro/post/00204.html
m.xnnbb.pro/post/22604.html
m.xnnbb.pro/post/13735.html
m.xnnbb.pro/post/86842.html
m.xnnbb.pro/post/53339.html
m.xnnbb.pro/post/77979.html
m.xnnbb.pro/post/77173.html
m.xnnbb.pro/post/53731.html
m.xnnbb.pro/post/97179.html
m.xnnbb.pro/post/75757.html
m.xnnbb.pro/post/13375.html
m.xnnbb.pro/post/59193.html
m.xnnbb.pro/post/19355.html
m.xnnbb.pro/post/17135.html
m.xnnbb.pro/post/79317.html
m.xnnbb.pro/post/95775.html
m.xnnbb.pro/post/79915.html
m.xnnbb.pro/post/26406.html
m.xnnbb.pro/post/35735.html
m.xnnbb.pro/post/79731.html
m.xnnbb.pro/post/57771.html
m.xnnbb.pro/post/22200.html
m.xnnbb.pro/post/75313.html
m.xnnbb.pro/post/99513.html
m.xnnbb.pro/post/88666.html
m.xnnbb.pro/post/71533.html
m.xnnbb.pro/post/33153.html
m.xnnbb.pro/post/88208.html
m.xnnbb.pro/post/75193.html
m.xnnbb.pro/post/99535.html
m.xnnbb.pro/post/39991.html
m.xnnbb.pro/post/91111.html
m.xnnbb.pro/post/93753.html
m.xnnbb.pro/post/73535.html
m.xnnbb.pro/post/62428.html
m.xnnbb.pro/post/95791.html
m.xnnbb.pro/post/99157.html
m.xnnbb.pro/post/17955.html
m.xnnbb.pro/post/93177.html
m.xnnbb.pro/post/95155.html
m.xnnbb.pro/post/11795.html
m.xnnbb.pro/post/73379.html
m.xnnbb.pro/post/53115.html
m.xnnbb.pro/post/60046.html
m.xnnbb.pro/post/55755.html
m.xnnbb.pro/post/86608.html
m.xnnbb.pro/post/35397.html
m.xnnbb.pro/post/53155.html
m.xnnbb.pro/post/46008.html
m.xnnbb.pro/post/51539.html
m.xnnbb.pro/post/80080.html
m.xnnbb.pro/post/55751.html
m.xnnbb.pro/post/71153.html
m.xnnbb.pro/post/15973.html
m.xnnbb.pro/post/11573.html
m.xnnbb.pro/post/08428.html
m.xnnbb.pro/post/39115.html
m.xnnbb.pro/post/15919.html
m.xnnbb.pro/post/57115.html
m.xnnbb.pro/post/71539.html
m.xnnbb.pro/post/59997.html
m.xnnbb.pro/post/40844.html
m.xnnbb.pro/post/28226.html
m.xnnbb.pro/post/99199.html
m.xnnbb.pro/post/48739.html
m.xnnbb.pro/post/11337.html
m.xnnbb.pro/post/88022.html
m.xnnbb.pro/post/91935.html
m.xnnbb.pro/post/31937.html
m.xnnbb.pro/post/82460.html
m.xnnbb.pro/post/79351.html
m.xnnbb.pro/post/71111.html
m.xnnbb.pro/post/99577.html
m.xnnbb.pro/post/59939.html
m.xnnbb.pro/post/77571.html
m.xnnbb.pro/post/66204.html
m.xnnbb.pro/post/71513.html
m.xnnbb.pro/post/17315.html
m.xnnbb.pro/post/71577.html
m.xnnbb.pro/post/79379.html
m.xnnbb.pro/post/93337.html
m.xnnbb.pro/post/17953.html
m.xnnbb.pro/post/19579.html
m.xnnbb.pro/post/57171.html
m.xnnbb.pro/post/44082.html
m.xnnbb.pro/post/82080.html
m.xnnbb.pro/post/93915.html
m.xnnbb.pro/post/35737.html
m.xnnbb.pro/post/35115.html
m.xnnbb.pro/post/66602.html
m.xnnbb.pro/post/11335.html
m.xnnbb.pro/post/48240.html
m.xnnbb.pro/post/80608.html
m.xnnbb.pro/post/39159.html
m.xnnbb.pro/post/13519.html
m.xnnbb.pro/post/93591.html
m.xnnbb.pro/post/93319.html
m.xnnbb.pro/post/33133.html
m.xnnbb.pro/post/31511.html
m.xnnbb.pro/post/15559.html
m.xnnbb.pro/post/79531.html
m.xnnbb.pro/post/82682.html
m.xnnbb.pro/post/11733.html
m.xnnbb.pro/post/99151.html
m.xnnbb.pro/post/26668.html
m.xnnbb.pro/post/20622.html
m.xnnbb.pro/post/13937.html
m.xnnbb.pro/post/91577.html
m.xnnbb.pro/post/37111.html
m.xnnbb.pro/post/75117.html
m.xnnbb.pro/post/79171.html
m.xnnbb.pro/post/95151.html
m.xnnbb.pro/post/40862.html
m.xnnbb.pro/post/37937.html
m.xnnbb.pro/post/00820.html
m.xnnbb.pro/post/59559.html
m.xnnbb.pro/post/15351.html
m.xnnbb.pro/post/62882.html
m.xnnbb.pro/post/53999.html
m.xnnbb.pro/post/77117.html
m.xnnbb.pro/post/19591.html
m.xnnbb.pro/post/73553.html
m.xnnbb.pro/post/46826.html
m.xnnbb.pro/post/53157.html
m.xnnbb.pro/post/73351.html
m.xnnbb.pro/post/33351.html
m.xnnbb.pro/post/06408.html
m.xnnbb.pro/post/97997.html
m.xnnbb.pro/post/15535.html
m.xnnbb.pro/post/53931.html
m.xnnbb.pro/post/73911.html
m.xnnbb.pro/post/00404.html
m.xnnbb.pro/post/48220.html
m.xnnbb.pro/post/00880.html
m.xnnbb.pro/post/77333.html
m.xnnbb.pro/post/31155.html
m.xnnbb.pro/post/17957.html
m.xnnbb.pro/post/22424.html
m.xnnbb.pro/post/00408.html
m.xnnbb.pro/post/15395.html
m.xnnbb.pro/post/39759.html
m.xnnbb.pro/post/35775.html
m.xnnbb.pro/post/59391.html
m.xnnbb.pro/post/73155.html
m.xnnbb.pro/post/31951.html
m.xnnbb.pro/post/39939.html
m.xnnbb.pro/post/42208.html