C++ 模板实例化
来源:互联网 发布:斗直播软件 编辑:程序博客网 时间:2024/06/05 08:25
16.2. Instantiation
A template is a blueprint; it is not itself aclass or a function. The compiler uses the template to generate type-specificversions of the specified class or function. The process of generatng a type-specificinstance of a template is known as instantiation. The term reflects the notion that a new"instance" of the template type or function is created.
A template is instantiated when we use it. Aclass template is instantiated when we refer to the an actual template classtype, and a function template is instantiated when we call it or use it to initializeor assign to a pointer to function.
Instantiating a Class
When wewrite
Queue<int>qi;
the compilerautomatially creates a class named Queue<int>. In effect, the compilercreates the Queue<int>class by rewriting the Queuetemplate, replacing everyoccurrence of the template parameter Typeby the type int. The instantiatedclass is as if we had written:
// simulated version of Queueinstantiated for type int
template <class Type> classQueue<int> {
public:
Queue(); // this bound toQueue<int>*
int &front(); // return typebound to int
const int &front() const; //return type bound to int
void push(const int &); //parameter type bound to int
void pop(); // type invariant code
bool empty() const; // type invariantcode
private:
// ...
};
Class Template Arguments Are Required
ClassTemplate Arguments Are Required
When we wantto use a class template, we must always specify the template arguments explicitly.
Queue qs; //error: which template instantiation?
A class template does not define a type; only aspecific instantiation defines a type. We define a specific instantiation byproviding a template argument to match each template parameter.
Templatearguments are specified in a comma-separated list and bracketed by the (<)and (>) tokens:
Queue<int>qi; // ok: defines Queue that holds ints
Queue<string>qs; // ok: defines Queue that holds strings
The typedefined by a template class always includes the template argument(s). Forexample, Queueis not a type; Queue<int>or Queue<string>are.
Function-Template Instantiation
When we usea function template, the compiler will usually infer the template arguments forus:
int main()
{
compare(1, 0); // ok: binds templateparameter to int
compare(3.14, 2.7); // ok: bindstemplate parameter to double
return 0;
}
This programinstantiates two versions of compare: one where Tis replaced by intand the otherwhere it is replaced by double.
- 模板实例化(c++)
- [C++]类模板在何时实例化?
- C++primer学习:类模板(2)类模板:模板参数,成员模板和控制实例化
- Linux C/C++ 模板:模板术语 模板参数/模板实参/模板名字/模板ID/特化/实例化
- C++primer学习:类模板(1):函数模板,模板参数,实例化
- C++ 使用结构类型实例化模板参数
- C++(33)模板与泛型之实例化
- C++_模板的实例化与具体化。
- C++模板实例化
- 模板的实例化
- 模板实例化事件
- 模板的实例化
- 模板实例化方式
- 模板实例化
- c++模板实例化
- 控制模板实例化
- C++模板实例化
- 模板实例化出错
- nodejs util utils傻傻分不清
- 机器学习笔记--回归分析(2)
- 关于多张图片分单张传递返回拼接字符串的问题
- linux c setitimer用法说明
- 浅谈JavaScript变量声明提升
- C++ 模板实例化
- ios 修改navigationBar标题颜色swift版
- P51 第二章 第六题 潘璠
- java中线程的协作sleep yield wait 和 notify
- 追逐自己的梦想----------辅助制作第二十七课:身上装备更换的分析与实现
- 【原创】MultiBitSet-BitSet扩展
- Android结构各层
- restrict关键字
- 简单选择排序