网站数据库一般多大,网站开发者不给源代码怎么办,北京建设部网站 信息中心,怎么自己做推广网站本篇我们来介绍更多关于C模板的知识。模板初阶移步至#xff1a;【C】模板#xff08;初阶#xff09;
1.非类型模板参数
1.1 非类型模板参数介绍
模板参数可以是类型形参#xff0c;也可以是非类型形参。类型形参就是我们目前接触到的一些模板参数。
//类型模板参数
…
本篇我们来介绍更多关于C模板的知识。模板初阶移步至【C】模板初阶
1.非类型模板参数
1.1 非类型模板参数介绍
模板参数可以是类型形参也可以是非类型形参。类型形参就是我们目前接触到的一些模板参数。
//类型模板参数
templateclass T, class Container vectorT, class Compare LessT非类型模板参数就是用一个常量作为模板的参数在模板中可以将该参数当作常量使用。
//N为非类型模板参数
templateclass T, size_t N 10 //N给缺省值
templatesize_t N //N没给缺省值
比如说我们要弄一个数组长度是固定的栈。
templatesize_t N
class Stack
{
private:int _a[N]; //定长数组//...
};
在使用的时候就可以传想要的N的值。
int main()
{Stack5 st1; //存5个数据Stack10 st2; //存10个数据return 0;
}
1.1.1 和C语言宏对比
这个和C语言的宏区别还是很大的C语言的宏只能存5个或者10个数据不可以像上面这样st1存5个值st2又能存10个数据。
#define N 5 //宏此时st1和st2都是存5个数据
class Stack
{
private:int _a[N]; //定长数组//...
};int main()
{Stack st1; Stack st2; return 0;
} 1.1.2 底层原理及注意事项
传非类型模板参数和传类型模板参数模板的底层原理都是一样的都是生成了不同的类如果是函数模板就是生成了不同的函数。
如果非类型模板参数给了缺省值可以不传参但是要加上。
templatesize_t N 5 //给缺省值
class Stack
{
private:int _a[N]; //定长数组//...
};int main()
{Stack st1; //不传参Stack10 st2; //传参return 0;
}
不传参也不加的写法在C20才支持这里还是建议加上。 注意 1.非类型模板参数只能用于整形bool也算整形浮点数C20才支持、类对象以及字符串是不允许作为非类型模板参数的。 2.. 非类型的模板参数必须在编译期就能确认结果。 我们也可以有多个非类型模板参数。
templatesize_t N 5, bool fg true //给缺省值1.2 array介绍
array是一个容器底层就是一个静态的数组它就用到了非类型模板参数。
相关文档array - C Reference 使用时包含头文件 #include array 第一个模板参数T是类型模板参数第二个模板参数N是非类型模板参数。 array支持迭代器也支持下标访问array就没有头删尾删、头插尾插这样的接口了因为它是定长的。
我们来用一下array假如要定义一个类型为int长度为10的数组。
arrayint, 10 a1;等同于 int a1[10];
array对于数组越界的检查是比较严格的比如下面的例子。
int a2[10];
cout a2[10] endl;//读越界的位置 未报错。
但是 array的越界检查是比较严格的会直接报错。 补充一句array的数据是存在栈上的vector的数据是存在堆上的。 2.模板的特化
我们在使用模板的时候有些情况下对于一些特殊类型的结果可能不是我们想要的。这时我们就可以使用模板的特化。
2.1 函数模板的特化
比如说我们用函数模板实现两个数的小于比较。
templateclass T
bool Less(T left, T right)
{return left right;
}
在通常情况下这个模板是没有问题的像传一些int、double这样的内置类型。但是如果传自定义类型可能结果就不是我们想要的比如说Date类我们就可以用模板的特化。 函数模板的特化步骤 1. 必须要先有一个基础的函数模板 2. 关键字 template 后面接一对 空的尖括号 3. 函数名后 跟一对尖括号尖括号中指定需要特化的类型 4. 函数形参表 : 必须要和模板函数的基础参数类型完全相同如果不同编译器可能会报一些奇怪的错误。 template
bool LessDate*(Date* left, Date* right)
{return *left *right;
}
当我们传参类型为Date*时就会直接走特化的模板传别的类型的参数时还是走函数模板那一套。 2.2 类模板的特化
2.2.1 全特化
全特化就是将模板参数列表中所有的参数都确定化。假如这里有一个如下的类。
templateclass T1, class T2
class Date
{
public:Date(){cout DateT1, T2 endl;}
};
对这个类全特化就是在类名后面加上里面写上具体类型。
template
class Dateint, char
{
public:Date(){cout Dateint, char endl;}
};
如果我们传int和char类型的参数过去就直接匹配调用特化了的类其余情况都调用正常的类模板。
int main()
{Dateint, int d1;Dateint, char d2;return 0;
} 2.2.2 偏特化半特化
偏特化有两种表现方式部分特化和参数更进一步的限制。
部分特化
这种特化就是只特化一部分如下。
templateclass T1
class DateT1, char
{
public:Date(){cout Dateint, char endl;}
};
只要是第二个参数传的是char类型就会匹配到这个偏特化半特化的类其余情况还是匹配正常的类模板。
int main()
{Dateint, int d1;Datechar, char d2;Datedouble, char d3;return 0;
} 当全特化和偏特化半特化同时存在时会优先选择全特化。
templateclass T1, class T2 //正常类模板
class Date
{
public:Date(){cout DateT1, T2 endl;}
};template //全特化
class Dateint, char
{
public:Date(){cout Dateint, char endl;}
};templateclass T1 //偏特化
class DateT1, char
{
public:Date(){cout DateT1, char endl;}
};int main()
{Dateint, char d2; //优先选择全特化return 0;
} 参数更进一步的限制
这种特化针对模板参数更进一步的条件限制所设计出来的一个特化版本。比如说下面这个。
templateclass T1, class T2
class DateT1*, T2* //指针
{
public:Date(){cout DateT1*, T2* endl;}
};
这个特化的意思就是只要参数传的是指针不管什么类型的指针都走这个模板。比如说下面的例子。
int main()
{Dateint, char d1;Dateint*, char* d2; //传的指针Datedouble*, char* d3; //传的指针Dateint*, double* d4; //传的指针Datechar*, int* d5; //传的指针return 0;
} 除了特化成指针还能特化成引用。
templateclass T1, class T2
class DateT1, T2 //引用
{
public:Date(){cout DateT1, T2 endl;}
};Dateint, char d6; //传的引用
Datedouble, char d7; //传的引用
Dateint, double d8; //传的引用
Datechar, int d9; //传的引用 两者混合也可以一个指针一个引用。
templateclass T1, class T2
class DateT1, T2*
{
public:Date(){cout DateT1, T2* endl;}
}; 易错点
我们先看下面这段代码。
templateclass T1, class T2
class DateT1*, T2*
{
public:Date(){cout DateT1*, T2* endl;T1 t;cout typeid(t).name() endl;}
};
int main()
{Dateint*, int* d1;
}
代码中的t是什么类型是 int* 还是 int 是int。
如果传过去的是int**t的类型就是int*。
Dateint**, int* d1; 在特化的时候除了指针这里会比较容易混淆引用也是一样比如下面这个例子。
templateclass T1, class T2
class DateT1, T2
{
public:Date(){cout DateT1, T2 endl;T1 t;cout typeid(t).name() endl;}
};
void test6()
{Dateint, int d1;
}此时t的类型就是int而不是int类型的引用。 所以我们想定义一个指针或者引用的时候应该要像下面这样。
templateclass T1, class T2
class DateT1, T2*
{
public:Date(){cout DateT1, T2* endl;int a 0;T1 t1 a; //定义引用T2* t2 a; //定义指针//...}
};
因为T1和T2在上面的情况下都是int类型而不是我们想要的引用和指针。 3.模板分离编译及优缺点
3.1 模板的分离编译
我之前的很多篇博客用到了声明和定义分离的方法主要是一些像模拟实现这样的代码较多的程序随着我们更深入的学习一个程序的代码量是会越来越多的声明和定义分离可以让我们的代码更有条理。 分离编译一个程序项目由若干个源文件共同实现而每个源文件单独编译生成目标文件最后将所有目标文件链接起来形成单一的可执行文件的过程称为分离编译模式。 程序要运行一般要经历这几个步骤预处理 - 编译 - 汇编 - 链接 而模板是不支持声明和定义分离的会发生链接错误。 所以建议模板的声明和定义放在同一个文件中不要分离。 3.2 模板的优缺点 【优点】 1. 模板复用了代码节省资源更快的迭代开发C的标准模板库(STL)因此而产生 2. 增强了代码的灵活性 【缺陷】 1. 模板会导致代码膨胀问题也会导致编译时间变长 2. 出现模板编译错误时错误信息非常凌乱不易定位错误 本次分享就到这里我们下篇再见~
文章转载自: http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.rwrn.cn.gov.cn.rwrn.cn http://www.morning.qyllw.cn.gov.cn.qyllw.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.xkzr.cn.gov.cn.xkzr.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.ztmkg.cn.gov.cn.ztmkg.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.vehna.com.gov.cn.vehna.com http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.nzms.cn.gov.cn.nzms.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.kfstq.cn.gov.cn.kfstq.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.dtrz.cn.gov.cn.dtrz.cn http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.wdhlc.cn.gov.cn.wdhlc.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn