官术网_书友最值得收藏!

Implicit instantiation

When referring to a template class, the compiler will only generate code from its template on-demand if it has not been explicitly instantiated or explicitly specialized. This is called implicit instantiation, and its syntax is as follows:

class_name<argument list> object_name; //for non-pointer object 
class_name<argument list> *p_object_name; //for pointer object

For a non-pointer object, a template class is instantiated and its object is created, but only the member functions used by this object are generated. For a pointer object, unless a member is used in the program, it is not instantiated.

Consider the following example, where we define a class template, X ,in the ch4_5_class_template_implicit_inst.h file:

//file ch4_5_class_template_implicit_inst.h
#ifndef __CH4_5_H__
#define __CH4_5_H__
#include <iostream>
template <class T>
class X {
public:
X() = default;
~X() = default;
void f() { std::cout << "X::f()" << std::endl; };
void g() { std::cout << "X::g()" << std::endl; };
};
#endif

Then, it is included by the following four cpp files, which has ain() in each:

//file ch4_5_class_template_implicit_inst_A.cpp
#include "ch4_5_class_template_implicit_inst.h"
void main() {
//implicit instantiation generates class X<int>, then create object xi
X<int> xi ;
//implicit instantiation generates class X<float>, then create object xf
X<float> xf;
return 0;
}

In ch4_5_class_template_implicit_inst_A.cpp, the compiler will implicitly instantiate the X<int> and X<float> classes, and then create the xi and xf objects. But since X::f() and X::g() are not used, they are not instantiated.

Now, let's look at ch4_5_class_template_implicit_inst_B.cpp:

//file ch4_5_class_template_implicit_inst_B.cpp
#include "ch4_5_class_template_implicit_inst.h"
void main()
{
//implicit instantiation generates class X<int>, then create object xi
X<int> xi;
xi.f(); //and generates function X<int>::f(), but not X<int>::g()

//implicit instantiation generates class X<float>, then create object
//xf and generates function X<float>::g(), but not X<float>::f()
X<float> xf;
xf.g() ;
}

Here, the compiler will implicitly instantiate the X<int> class, create the xi object, and then generate the X<int>::f() function, but not X<int>::g(). Similarly, it will instantiate the X<float> class, create the xf object, and generate the X<float>::g() function, but not X<float>::f().

Then, we have ch4_5_class_template_implicit_inst_C.cpp:

//file ch4_5_class_template_implicit_inst_C.cpp
#include "ch4_5_class_template_implicit_inst.h"
void main()
{
//inst. of class X<int> is not required, since p_xi is pointer object
X<int> *p_xi ;
//inst. of class X<float> is not required, since p_xf is pointer object
X<float> *p_xf ;
}

Since p_xi and p_xf are pointer objects, there is no need to instantiate their corresponding template classes through the compiler.

Finally, we have ch4_5_class_template_implicit_inst_D.cpp:

//file ch4_5_class_template_implicit_inst_D.cpp
#include "ch4_5_class_template_implicit_inst.h"
void main()
{
//inst. of class X<int> is not required, since p_xi is pointer object
X<int> *p_xi;

//implicit inst. of X<int> and X<int>::f(), but not X<int>::g()
p_xi = new X<int>();
p_xi->f();

//inst. of class X<float> is not required, since p_xf is pointer object
X<float> *p_xf;
p_xf = new X<float>();//implicit inst. of X<float> occurs here
p_xf->f(); //implicit inst. X<float>::f() occurs here
p_xf->g(); //implicit inst. of X<float>::g() occurs here

delete p_xi;
delete p_xf;
}

This will implicitly instantiate X<int> and X<int>::f(), but not X<int>::g(); similarly, for X<float>, X<float>::f() and X<float>::g() will be instantiated.

主站蜘蛛池模板: 德昌县| 靖宇县| 扶沟县| 吴忠市| 蓬溪县| 绥德县| 章丘市| 天镇县| 香格里拉县| 新兴县| 乌拉特中旗| 宁武县| 桂阳县| 冀州市| 仙桃市| 商南县| 长乐市| 玉溪市| 芒康县| 玉田县| 岑巩县| 永年县| 辽中县| 肥乡县| 新巴尔虎右旗| 大竹县| 屏东县| 梨树县| 黑水县| 牟定县| 鲁甸县| 普宁市| 八宿县| 乌海市| 逊克县| 太湖县| 克山县| 南昌县| 磐石市| 黄骅市| 昂仁县|