One of the great features of modern C++ is templates. A template is a simple and very powerful statement in C++ that defines the operations of a class or function, and Template Instantiation is to create a new definition of a function, class, or member of a class from template declaration with template argument types. In this article, we will explain Explicit Instantiation of a Template in C++ examples which can be used by a professional C++ Editor and compiler.
First of all, let’s try to explain what a template in C++ is.
Table of Contents
What is a template in C++?
A template is a very powerful statement in C++ that simply defines the operations of a class, a function, an alias, or a variable. It lets the user apply the same template on different variable types in those template operations. Templates are like macros in C++, considered in compilation except the compiler checks the types used before the template is expanded. In the compilation mechanism of a template in C++, the source code contains only a template for a function or class, but when it is compiled, the same template can be used on multiple data types.
For example, here is a function template that adds two a
and b
parameters of type T
:
1 2 3 4 5 6 7 |
template <class T> T add (T a, T b) { return a+b; } |
for example, if a
and b
are int
variables, this template can be used as below
1 2 3 |
int i = add <int> (a, b); |
and in the next lines same template can be used for the x
and y
float
variables as below too,
1 2 3 |
float f = add <float> (x, y); |
Templates are a powerful entity that can be parameterized by one or more template parameters. These parameters can be a type template parameter, a non-type template parameter, or a template as a template parameter.
What is template instantiation in C++?
Template Instantiation is to create a new definition of a function, class, or member of a class from a template declaration with template argument types. For example, a class template is not a type or an object, or any other entity, this does not generate a code from a source file that contains only a template definition. When instantiated, the code of a template appears.
Template instantiation can be done in two different ways:
- Explicit instantiation
- Implicit instantiation
In this article, we will explain what is explicit template instantiation,
What is explicit template instantiation in C++?
When we define a template class where it will work with a couple of explicit types, we declare the template in the header file like a normal class declaration. We put template definitions in a source file as in normal class definitions. Then we explicitly instantiate only the version of our template that we want.
Syntax of a definition an explicit Instantiation of a template:
1 2 3 |
template class_key template_name < arguments > ; |
Syntax of a declaration of an explicit instantiation of a template:
1 2 3 |
extern template class_key template_name < arguments > ; // since C++11 |
Note that, here the extern
keyword in the specialization is applied to the member functions defined outside of the body of the class. Functions that are defined inside the class declaration are considered inline functions and they are instantiated.
For example, if you want to define a template that has 20 integer variables – for example, ages – then you can declare ages like this:
1 2 3 |
template class ages<int, 20>; |
Where is there a simple explicit template instantiation example in C++?
Here is a simple class template explicit instantiation example in C++.
1 2 3 4 5 6 7 8 9 10 |
// Class Template Example template <class T> class Weight { }; // Template Instantiation template class Weight<int>; // explicit instantiation of Weight class template template class Weight<double>; // explicit instantiation of Weight class template |
Is there a full explicit template instantiation example in C++?
Here is an example of a class template and explicit template instantiation of this class template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> // Class Template Example template <class T> class Weight { private: // A variable for a value of type T T val; public: Weight(T n) : val(n) { } // constructor T value() // method to obtain value { return val; } }; // Template Instantiation template class Weight<int>; // explicit instantiation of Weight class template template class Weight<double>; // explicit instantiation of Weight class template int main() { // Class Template Usage Weight<double> car_weight(1650.2); std::cout << "Weight of the Car " << car_weight.value() << " kg" << std::endl; system("pause"); return 0; } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition