- Expert C++
- Vardan Grigoryan Shunguang Wu
- 189字
- 2021-06-24 16:34:07
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.
- Vue 3移動Web開發(fā)與性能調(diào)優(yōu)實戰(zhàn)
- JBoss Weld CDI for Java Platform
- OpenCV實例精解
- Learning Firefox OS Application Development
- 你不知道的JavaScript(中卷)
- 程序員修煉之道:通向務(wù)實的最高境界(第2版)
- Salesforce Reporting and Dashboards
- Julia高性能科學(xué)計算(第2版)
- Mastering Akka
- 區(qū)塊鏈技術(shù)進(jìn)階與實戰(zhàn)(第2版)
- Scratch趣味編程:陪孩子像搭積木一樣學(xué)編程
- 并行編程方法與優(yōu)化實踐
- JQuery風(fēng)暴:完美用戶體驗
- SAS編程演義
- Splunk Essentials