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 a template declaration with template argument types. In this article, we will explain the Implicit Instantiation of a Template in C++ with examples that can be used by a professional C++ Tool such as an IDE and compiler.
First of all, let’s try to explain what templates in C++ are.
Table of Contents
What is 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 and let the user may apply the same template on different types in those template operations. Templates are like macros in C++, considered in compilation except compiler checks types used before the template is expanded in its terms. 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 in T type
,
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 powerful features of C++ 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 parameter.
What is template instantiation in C++?
Template Instantiation means 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 the implicit instantiation of a template in C++?
The Implicit Instantiation of a template (or Implicit Template Instantiation) occurs when a template is used to declare a pointer variable or a variable with the provided arguments. In this situation, the compiler automatically generates the concrete class or function for the provided template arguments.
To do implicit instantiation, first, we should define a template, then the compiler instantiates a class template specialization and the template is declared implicitly.
Where is a simple implicit instantiation of template example in C++?
Here is a simple implicit class template instantiation example in C++.
1 2 3 4 5 6 7 8 |
// Class Template Example template <class T> class Weight { }; Weight<double> car_weight(1650.2); // implicit instantiation of Weight<double> |
Is there a full implicit instantiation of template example in C++?
Here is a example about class template and implicit 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); // implicit instantiation of Weight<double> 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