码迷,mamicode.com
首页 > 其他好文 > 详细

函数模板与模板函数及模板类与模板的特化

时间:2016-04-25 06:51:54      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

函数模板( Function templates)

      * 模板(Templates)使得我们可以生成通用的函数,这些函数能够接受任意数据类型的参数,可返回任意类型的值,而不需要对所有可能的数据类型进行函数重载。这在一定程度上实现了宏(macro)的作用。它们的原型定义可以是下面两种中的任何一个:

template <class identifier> function_declaration;
template <typename identifier>
 function_declaration;

上面两种原型定义的不同之处在关键字class 或 typename的使用。它们实际是完全等价的,因为两种表达的意思和执行都一模一样

如何编写一个通用加法函数?
  • 使用函数重载,针对每个所需相同行为的不同类型重新实现它
<span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">int</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">Add(</span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> &</span><span style="font-size:18px;color:#cc33cc;">_iLeft,</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> &</span><span style="font-size:18px;color:#cc33cc;">_iRight)</span></span><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">     </span><span style="font-size:18px;color:#cc33cc;">return</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">(_iLeft + _iRight);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">float</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">Add(</span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">float</span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">&</span></span><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">_fLeft,</span></span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">const</span></span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">float</span></span><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">&</span></span><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">_fRight)</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">     </span><span style="font-size:18px;color:#cc33cc;">return</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">(_fLeft + _fRight);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div>


【缺点】
  1、只要有新类型出现,就要重新添加对应函数。
  2、除类型外,所有函数的函数体都相同,代码的复用率不高
  3、如果函数只是返回值类型不同,函数重载不能解决
  4、一个方法有问题,所有的方法都有问题,不好维护。
  • 使用公共基类,将通用的代码放在公共的基础类里面
 【缺点】
  1、借助公共基类来编写通用代码,将失去类型检查的优点;
  2、对于以后实现的许多类,都必须继承自某个特定的基类,代码维护更加困难。
  • 使用特殊的预处理程
<span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">#define</span><span style="font-size:18px;color:#cc33cc;">  ADD(a, b) ((a) + (b))</span></span><span style="font-family: Times New Roman;">
</span>
  • 泛型编程
泛型编程:编写与类型无关的逻辑代码,是代码复用的一种手段。模板是泛型编程的基础
技术分享
  • 函数模板
函数模板:代表了一个函数家族,该函数与类型无关,在使用时被参数化,根据实参类型产生函数的特定类型版本。

模板函数的格式
template<typename Param1, typename Param2,...,class Paramn>
返回值类型  函数名(参数列表)
{
    ...
}
技术分享

typename是用来定义模板参数关键字,也可以使用class。建议尽量使用typename。
注意:不能使用struct代替typename。

模板函数也可以定义为inline函数
<span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T></span></span><div align="left"><div align="left"><div align="left"><div align="left"><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">inline</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T Add(</span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T _left,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T _right)</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">     </span><span style="font-size:18px;color:#cc33cc;">return</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">(_left + _right);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div></div></div></div></div>

注意:inline关键字必须放在模板形参表之后,返回值之前,不能放在template之前
模板是一个蓝图,它本身不是类或者函数,编译器用模板产生指定的类或者函数的特定类型版本,产生模板特定类型的过程称为函数模板实例化。
技术分享
注意:模板被编译了两次:
  • 实例化之前,检查模板代码本身,查看是否出现语法错误,如:遗漏分号
  • 在实例化期间,检查模板代码,查看是否所有的调用都有效,如:实例化类型不支持某些函数调用
【实参推演】
从函数实参确定模板形参类型和值的过程称为模板实参推断
多个类型形参的实参必须完全匹配
【类型形参转换】
一般不会转换实参以匹配已有的实例化,相反会产生新的实例。
编译器只会执行两种转换:
1、const转换:接收const引用或者const指针的函数可以分别用非const对象的引用或者指针来调用
2、数组或函数到指针的转换:如果模板形参不是引用类型,则对数组或函数类型的实参应用常规指针转换。数组实参将当做指向其第一个元素的指针,函数实参当做指向函数类型的指针。
  • 模板参数
函数模板有两种类型参数:模板参数和调用参数
技术分享
模板形参名字只能在模板形参之后到模板声明或定义的末尾之间使用,遵循名字屏蔽规则
技术分享
模板形参的名字在同一模板形参列表中只能使用一次
技术分享
所有模板形参前面必须加上class或者typename关键字修饰
技术分享
注意:在函数模板的内部不能指定缺省的模板实参。
下面模板函数声明有问题吗?
<span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">class</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T, U,</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">typename</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">V></span></span><div align="left"><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">void</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">f1(T, U, V);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="color:#cc33cc;"></span><span style="font-size:18px;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">class</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">T f2(</span><span style="font-size:18px;color:#cc33cc;">int</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">&T);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="color:#cc33cc;"></span><span style="font-size:18px;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">class</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T></span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">T f3 (T, T);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="color:#cc33cc;"></span><span style="font-size:18px;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">typedef</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">int</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">TYPENAME;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">TYPENAME></span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">TYPENAME f4(TYPENAME);</span></span></div></div>

【非模板类型参数】
  非模板类型形参是模板内部定义的常量,在需要常量表达式的时候,可以使用非模板类型参数。

例如数组长度
技术分享

类型等价性
const int iByteCnt = 9;
<div align="left"><div align="left"><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">   </span><span style="font-size:18px;color:#cc33cc;">int</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">b[iByteCnt+1];</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">   </span><span style="font-size:18px;color:#cc33cc;">int</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">a[10];</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">   FunTest(a);</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">// FunTest<int, 10>  两个数组等价</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">   FunTest(b);</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">// FunTest<int, 10>  编译器不会合成新的函数</span></span></div></div></div>

模板形参说明
1、模板形参表使用<>括起来
2、和函数参数表一样,跟多个参数时必须用逗号隔开,类型可以相同也可以不相同
3、模板形参表不能为空
4、模板形参可以是类型形参,也可以是非类型新参,类型形参跟在class和typename后
5、模板类型形参可作为类型说明符用在模板中的任何地方,与内置类型或自定义类型
   使用方法完全相同,可用于指定函数形参类型、返回值、局部变量和强制类型转换
6、模板形参表中,class和typename具有相同的含义,可以互换,使用typename更加直观。
   但关键字typename是作为C++标准加入到C++中的,旧的编译器可能不支持。

  • 模板函数重载
<span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">int</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">Max(</span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">& left,</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">int</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">& right)</span></span><div align="left"><div align="left"><div align="left"><div align="left"><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">     </span><span style="font-size:18px;color:#cc33cc;">return</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">left>right? left:right;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">T Max(</span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T& left,</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T& right)</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">        </span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">return</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">left>right? left:right;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">T Max(</span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T& a,</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T& b,</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">const</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">T& c)</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;">return</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">Max(Max(a, b), c);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">int</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">main()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;">Max(10, 20, 30);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">    Max<>(10, 20);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">    Max(10, 20);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">    Max(10, 20.12);</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    Max<</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">>(10.0, 20.0);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">    Max(10.0, 20.0);</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;">return</span><span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="font-size:18px;color:#cc33cc;">0;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div></div></div></div></div>


注意:函数的所有重载版本的声明都应该位于该函数被调用位置之前。

【说明】
   1、一个非模板函数可以和一个同名的函数模板同时存在,而且该函数模板还可以被实例 
      化为这个非模板函数
   2、对于非模板函数和同名函数模板,如果其他条件都相同,在调动时会优先调动非模板 
      函数而不会从该模板产生出一个实例。如果模板可以产生一个具有更好匹配的函数, 
      那么将选择模板。
   3、显式指定一个空的模板实参列表,该语法告诉编译器只有模板才能来匹配这个调用,   
      而且所有的模板参数都应该根据实参演绎出来。
   4、模板函数不允许自动类型转换,但普通函数可以进行自动类型转换。

问题:上面的模板还是可以比较两个字符串吗?

【模板函数特化】
 有时候并不总是能够写出对所有可能被实例化的类型都最合适的模板,在某些情况下,通用模板定义对于某个类型可能是完全错误的,或者不能编译,或者做一些错误的事情
技术分享

可以下面这样来定义
技术分享

模板函数特化形式如下:
1、关键字template后面接一对空的尖括号<>
2、再接模板名和一对尖括号,尖括号中指定这个特化定义的模板形参
3、函数形参表
4、函数体
template<>
返回值 函数名<Type>(参数列表)
{
      // 函数体
}

特化的声明必须与特定的模板相匹配,否则
技术分享

假如少了模板形参表?
<span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">int</span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">compare(</span></span><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">int</span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">v1,</span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">int</span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">v2)</span></span><div align="left"><div align="left"><div align="left"><div align="left"><div align="left"><div align="left"><div align="left"><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">          </span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">return</span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">0;</span></span></div><div align="left"><span style="font-family: Times New Roman;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div></div></div></div></div></div></div>
只是定义了一个普通函数,该函数含有返回类型和可与模板实例化相匹配的形参表。

注意:在模板特化版本的调用中,实参类型必须与特化版本函数的形参类型完全匹配,
      如果不匹配,编译器将为实参模板定义中实例化一个实例。
技术分享


注意:特化不能出现在模板实例的调用之后应该在头文件中包含模板特化的声明,然
      后使用该特化版本的每个源文件包含该头文件。

  • 模板类
【普通顺序表】
<div align="left"><div align="left"><div align="left"><span style="font-family:宋体;color:#800080;"><span style="font-size:18px;background-color: rgb(204, 51, 204);"></span></span><div align="left"><a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><span style="font-family:宋体;"><span style="color:#0000ff;"></span></span></div><span style="background-color: rgb(204, 51, 204);"></span><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">typedef</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">DataType</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><div align="left"><div align="left"><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">//typedef char DataType;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:     </span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">DataType</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span>
</div></div></div></div></div></div></div>

模板类也是模板,必须以关键字template开头,后接模板形参表。

【模板类格式】
<a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><div align="left"><div align="left"><div align="left"><div align="left"><div align="left"><div><span style="font-family:宋体;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">template</span></span><span style="font-size:18px;color:#cc33cc;"><</span><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">class</span></span><span style="font-size:18px;color:#cc33cc;"> </span></span><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">形参名1</span></span><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">, </span></span><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">class</span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">形参名2, ...</span></span></span></span><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">class</span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">形参名n</span></span></span><span style="font-size:18px;color:#cc33cc;">>  </span></span></div><div><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;"> </span><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">class</span></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="margin: 0px; padding: 0px;"><span style="font-size:18px;color:#cc33cc;">类名</span></span></span></span></span></div><div style="margin: 10px auto; padding: 0px; widows: 1; background-color: rgb(255, 255, 255);"><div><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;"> { ... };</span></span></span></span></span></div><div><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;"><span style="font-family:宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></span></span></span></div></div></div></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div></div></div><div><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">// 以模板方式实现动态顺序表</span></span></div><div align="left"><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    ~</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">>::</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    :</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;">(0)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    ,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;">(10)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    ,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">new</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">[</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;">])</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">>::~</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">delete</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">[]</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">test1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">sl1</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">double</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">sl2</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div></div></div>

【模板类的实例化】
只要有一种不同的类型,编译器就会实例化出一个对应的类。
 SeqList<int > sl1;
 SeqList<double > sl2;
当定义上述两种类型的顺序表时,编译器会使用int和double分别代替模板形参,重新编写
SeqList类,最后创建名为SeqList<int>和SeqList<double>的类。

  • 模板参数--实现容器适配器
<a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">// template <class T, class Container></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Container</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">=</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">> ></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">缺省参数</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Stack</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Push</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">x</span><span style="font-size:18px;color:#cc33cc;">);</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Pop</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">& </span><span style="font-size:18px;color:#cc33cc;">Top</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">bool</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Empty</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Container</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_con</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Test</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Stack</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">s1</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Stack</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">>></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">s2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">思考下面这种使用场景会怎样?</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Stack</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">char</span><span style="font-size:18px;color:#cc33cc;">>></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">s3</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div>

  • 模板的模板参数--容器适配器
<a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">// template <class T, template<class> class Container></span></span></div><div align="left"><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Container</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">=</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">缺省参数</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Stack</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Push</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">x</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">);</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Pop</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Top</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">bool</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Empty</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Container</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_con</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Test</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Stack</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">s1</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Stack</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">s2</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div>

技术分享

  • 非类型的类模板参数
// 静态顺序表
<a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><div align="left"><div align="left" style="font-size: 24px;"><div align="left"><span style="font-family:宋体;color:#800080;"><a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a></span><div align="left"><div align="left"><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">//template<typename T, size_t MAX_SIZE></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">size_t</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">MAX_SIZE</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">= 10></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;">带缺省模板参数</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_array</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">[</span><span style="font-size:18px;color:#cc33cc;">MAX_SIZE</span><span style="font-size:18px;color:#cc33cc;">];</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">size_t</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">MAX_SIZE</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">MAX_SIZE</span><span style="font-size:18px;color:#cc33cc;">>::</span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    :</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;">(0)</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Test</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">s1</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">, 20></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">s2</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div></div><span style="font-family:宋体;color:#800080;"></span><div align="left"><div align="left" style="font-size: 24px;">
</div></div><div align="left"><div align="left" style="font-size: 24px;"><div align="left" style="font-size:14px;">
</div></div><div align="left" style="font-size: 24px;"><div align="left" style="font-size:14px;"></div></div></div><div align="left" style="font-size: 24px;"><div align="left" style="font-size:14px;"><div align="left" style="font-size: 24px;"></div></div></div><div align="left" style="font-size: 24px;">
</div></div></div></div>
注意:浮点数和类对象是不允许作为非类型模板参数的
<a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><div align="left"><div align="left"><div align="left"><div align="left"><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">//template<class T, string name></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">double</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">MaxSize</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Test</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">double</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_value</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div></div></div></div>
全特化
<a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><div align="left"><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    ~</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">>::</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    :</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;">(0)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    ,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;">(10)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    ,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">new</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">[</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;">])</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">cout</span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">"SeqList<T>"</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">endl</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T</span><span style="font-size:18px;color:#cc33cc;">>::~</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">delete</span><span style="font-size:18px;color:#cc33cc;">[]</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">capacity</span><span style="font-size:18px;color:#cc33cc;">);</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    ~</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">特化后定义成员函数不再需要模板形参</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">>::</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">capacity</span><span style="font-size:18px;color:#cc33cc;">)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">         :</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_size</span><span style="font-size:18px;color:#cc33cc;">(0)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">         ,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">capacity</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">         ,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">new</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">[</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_capacity</span><span style="font-size:18px;color:#cc33cc;">])</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">cout</span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">"SeqList<int>"</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">endl</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">特化后定义成员函数不再需要模板形参</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">>::~</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">delete</span><span style="font-size:18px;color:#cc33cc;">[]</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">test1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">double</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">sl2</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">SeqList</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">sl1</span><span style="font-size:18px;color:#cc33cc;">(2);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div></div>

  • 偏特化(局部特化)
<a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><div align="left"><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">>::</span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">cout</span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">"Data<T1, T2>"</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">endl</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">局部特化第二个参数</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">>::</span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;">cout</span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">"Data<T1, int>"</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">endl</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div></div><span style="font-size:18px;"></span>
ps:下面的例子可以看出,偏特化并不仅仅是指特殊部分参数,而是针对模板参数更进一步的条件限制所设计出来的一个特化版本
<a target=_blank name="1455" style="color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 13.3333px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></a><div align="left"><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">局部特化两个参数为指针类型</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">*,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">*></span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;">();</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d3</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d4</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">*,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">*>::</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">cout</span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">"Data<T1*, T2*>"</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">endl</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">//</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">局部特化两个参数为引用</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">class</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">&,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">&></span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">public</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">d1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">d2</span><span style="font-size:18px;color:#cc33cc;">);</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">private</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">:</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d1</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d2</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d3</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">*</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d4</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">};</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">template</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">typename</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">></span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">&,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">&>::</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T1</span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">d1</span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">const</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">T2</span><span style="font-size:18px;color:#cc33cc;">&</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">d2</span><span style="font-size:18px;color:#cc33cc;">)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    :</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d1</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">d1</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">)</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    ,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">_d2</span><span style="font-size:18px;color:#cc33cc;">(</span><span style="font-size:18px;color:#cc33cc;">d2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">)</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">cout</span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">"Data<T1&, T2&>"</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;"><<</span><span style="font-size:18px;color:#cc33cc;">endl</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;"></span><span style="color:#cc33cc;"></span>
</span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">void</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">test2</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">()</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">{</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">double</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">d1</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">double</span><span style="font-size:18px;color:#cc33cc;">></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">d2</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">*,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">*></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">d3</span><span style="font-size:18px;color:#cc33cc;">;</span></span></div><div align="left"><span style="font-family:宋体;"><span style="font-size:18px;color:#cc33cc;">    </span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">Data</span><span style="font-size:18px;color:#cc33cc;"><</span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">&,</span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">int</span><span style="font-size:18px;color:#cc33cc;">&></span><span style="font-size:18px;color:#cc33cc;"> </span><span style="font-size:18px;color:#cc33cc;">d4</span><span style="font-size:18px;color:#cc33cc;">(1, 2);</span></span></div><div align="left"><span style="font-family: 宋体;"><span style="font-size:18px;color:#cc33cc;">}</span></span></div></div>

模板的全特化和偏特化都是在已定义的模板基础之上,不能单独存在。

  • 模板的分离编译
技术分享

解决办法:
  1. 在模板头文件 xxx.h 里面显示实例化->模板类的定义后面添加 template class SeqList<int >; 一般不推荐这种方法,一方面老编译器可能不支持,另一方面实例化依赖调用者。(不推荐)
  2. 将声明和定义放到一个文件 "xxx.hpp" 里面,推荐使用这种方法

【分离编译的扩展阅读】

  • 模板总结
【优点】
  模板复用了代码,节省资源,更快的迭代开发,C++的标准模板库(STL)因此而产生。
  增强了代码的灵活性。

【缺点】
  模板让代码变得凌乱复杂,不易维护,编译代码时间变长。
  出现模板编译错误时,错误信息非常凌乱,不易定位错误。

模板还有一些高深晦涩的玩法,在实际运中的大多数的场景下上面的特性已经够我们玩了,所以不用去纠结模板的复杂莫测的玩法。真有兴趣深入学习的童鞋,或者有要用到一些复杂特性时,可以通过《C++ Templates 中文版》学习。

函数模板与模板函数及模板类与模板的特化

标签:

原文地址:http://blog.csdn.net/yc2zgh1314/article/details/51236302

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!