c++ template实例化

来源:互联网 发布:网络诈骗需要什么证据 编辑:程序博客网 时间:2024/05/21 09:13

class template 实例化

实例化包括两种类型,隐式实例化,显示实例化

template的定义是什么

template的定义在编译过程中什么都不会生成,既不是类型,也不是对象,仅仅是声明。

显示实例化

语法:

template class name < argument-list > ;

这样的实例化,仅仅是定义了class,class的成员函数,以及函数的定义都没有实例化。

隐式实例化

只有在用到该类型或对象时,才会实例化

example

template<class T> struct Z {    void f() {}    void g(); // never defined}; // template definitiontemplate struct Z<double>; // explicit instantiation of Z<double>Z<int> a; // implicit instantiation of Z<int>Z<char>* p; // nothing is instantiated herep->f(); // implicit instantiation of Z<char> and Z<char>::f() occurs here.// Z<char>::g() is never needed and never instantiated: it does not have to be defined
0 0
原创粉丝点击