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. Template Specialization is used to get a special behavior from a template declaration for a particular data type. In this article, we will explain Template Specialization with C++ examples which can be used by a professional C++ Developer Tool that supports C++14, C++17, and over.
First of all, let’s try to explain what a template in C++ is.
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. 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.
Templates are 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 specialization in C++?
Template Specialization is used to get a special behavior from a template declaration for a particular data type. This allows you to define different behaviors for different data type usages in the same template. For example, let’s assume you have a template function that works with double
or float
inputs but when a user uses int
inputs it will give errors. In this situation, you should add a template specialization for that template when int
values are used as a parameter.
Syntax of a template specialization:
1 2 3 |
template <> special_declaration |
Is there a simple template specialization example in C++?
Here is a simple template specialization example that is used to force of a given mass.
1 2 3 4 5 6 7 8 9 10 11 |
template <class T> void force(T m) { std::cout << "Force:" << m*9.80665 << " Nt" << std::endl; } template <> void force(int m) // Template specialization for int types { std::cout << "This is specialized template: do not use integer " << m << std::endl; } |
Is there a full template specialization example in C++?
Here is a full example
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 |
#include <iostream> // Template Specialization Example template <class T> void force(T m) { std::cout << "Force:" << m*9.80665 << " Nt" << std::endl; } template <> void force(int m) // Template specialization for int types { std::cout << "This is specialized template: do not use integer " << m << std::endl; } int main() { double mass=68.987; force(80.5); force(mass); force(100); // invoke specialized template by using int input system("pause"); return 0; } |
and the output will be as follows,
1 2 3 4 5 6 |
Force:789.435 Nt Force:676.531 Nt This is specialized template: do not use integer 100 Press any key to continue . . . |
as you see we specialized our force()
template function for the int
types. If a user uses float
or double
values it runs as in the template, if a user uses an integer as an input, it warns the user not to use integer values as in our specialized template.
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