Template feature in C++ is one of the great features of modern C++. One of the great features of modern C++ is the ability to define templates. Templates are one of the most important techniques for you to learn, since it’s really only with knowledge of templates that you can claim to understand C++. So much of C++, including the standard library that you use every day as a C++ programmer, is built around the use of templates, and it’s very useful to be able to write them yourself too. In this article, we will explain how you can use type template parameters without a default in C++. To try them for yourself you can download a C++ IDE and compiler that supports the C++11, C++14, and C++17 standards.
Table of Contents
What is a template in C++?
A template is a powerful and very commonly C++ technique which lets you write code once, but have the same code apply to (be instantiated for) different types. This avoids writing the same thing multiple times. A simple example is when you define a vector: std::vector<int>
or std::vector<double>
. The template parameter, here the types int or double, result in different code being generated. The author has written the vector code once, but the compiler generates different results. This technique is called template metaprogramming.
Here is the syntax of a template:
1 2 3 |
template < parameters > declaration |
We can declare a template as below:
1 2 3 4 5 |
template<class T> class my_temp { }; |
What are the type template parameters in C++?
The parameters of a template can be,
- type template parameter,
- non-type template parameter,
- template template parameter (a template passed as a parameter to a template).
A type parameter can be one of the following types,
- typename
- class
- concept (since C++20)
In this post we will explain type template parameters in C++,
Here are the type template parameters in C++
Type template parameter without a default
In general, when we declare a template we use type template parameter without a default, this is the most general usage of type parameter in templates.
Here is a template syntax that uses the ‘template parameter without a default’:
1 2 3 |
template < type_parameter_key name(optional) > declaration |
Here are more details about how to use it:
Type template parameter with a default
Sometimes, when we declare a template we use a type template parameter with a default. In this way, you can easily use templates with default option, or you can define your class name.
Here is a template syntax that that uses the ‘template parameter with a default’:
1 2 3 |
template < type_parameter_key name(optional) = default > declaration |
Here are more details about how to use it:
Type template parameter pack (C++11)
Sometimes, when we use a template, we may use several template arguments, some of which may be optional. A template parameter pack is a template parameter with … (3 dots) ellipsis symbol that can be used to indicate optional template arguments (non-types, types, or templates) or it can be zero. The parameter pack symbol can be used as a function parameter pack too, it can also be zero or it may have multiple optional function arguments.
Note that, if a template has at least one parameter pack, this template is called as variadic template.
Here is a template syntax that uses the ‘type template parameter pack’:
1 2 3 |
template < type_parameter_key ... name(optional) > declaration |
Here are more details about how to use it.
Here are other new type template parameters which can be used in C++ 20.
What is a constrained type template parameter without a default (C++20)?
Since C++ 20 there is constrained type that we can use as a template parameter without a default.
Here is a template syntax that uses constrained type template parameter without a default:
1 2 3 |
template< type_constraint_name(optional) > declaration |
What is a constrained type template parameter with a default (C++20)?
Since C++ 20 there is constrained type that we can use as a template parameter with a default.
Here is a template syntax that uses constrained type template parameter without a default,
1 2 3 |
template< type_constraint_name(optional) = default > declaration |
What is a constrained type template parameter pack (C++20)?
Since C++ 20 there is constrained type that we can use as a template parameter pack.
Here is a template syntax that uses constrained type template parameter pack.
1 2 3 |
template< type_constraint ... name(optional) = default > declaration |
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