One of the great features of modern C++ is the ability to define templates. A template is a simple and very powerful statement in C++ that defines the operations of a class or function. In this article, we will explain type parameters with a default for templates in C++. You can download C++ here and use a professional C++ IDE and compiler to try things out for yourself.
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 types to increase code reuse. Templates are like macros in C++, 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.
Here is the syntax of a template:
1 2 3 |
template < parameters > declaration |
What are type parameters for templates in C++?
A template has at least one template parameter.
The parameters of a template can be,
- type template parameter,
- non-type template parameter,
- template template parameter (a template used as a parameter).
A type template parameter (typename or class) is a type parameter key provided within a template parameters list and it is a typename or class. Thus, user may use different type names or classes for the different declarations with templates. Here is a syntax for a template that has type parameter,
What is type template parameter with a default in C++?
A type template parameter (typename or class) is a type parameter key provided within a template parameters list, and it is a typename or class. The user may use different type names or classes for the different declarations with templates. Here is a syntax for a template that has type parameter,
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 the syntax for a C++ template parameter with a default.
1 2 3 |
template < type_parameter_key name(optional) = default > declaration |
A type parameter can be one of the following types,
- typename
- class
- concept (since C++20)
For example, we can declare a template with a void default as below,
1 2 3 4 5 |
template<class T = void> class my_temp { }; |
name is optional so if you don’t need class name in your template you can use without class name as below too,
1 2 3 4 5 |
template<class = void> class my_temp { }; |
Is there a C++ example of a type template parameter with a default?
When we create a template with a type parameter, we can define its default type (for example a void, or a class, or a typename) as shown below.
Here is an example of a C++ template parameter with a default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> // class name as a type parameter for a template template<class T = void> class my_temp { // template features here }; // a simple class declaration class myclass { }; int main() { // a class declaration with a template class my_temp< myclass > mytempclass; // a class declaration with a default of template my_temp<> mytempclass2; } |
Is there a C++ VCL example of a type template parameter with a default?
In C++ Builder, you can use the example above as console application.
Here is a full C++ VCL console example to the ‘template parameter with a default’ in C++ Builder.
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 |
#include <vcl.h> // Type Name Parameter for Class Template // class name as a type parameter for a template template<class T = void> class my_temp { // template features here }; // a simple class declaration class myclass { }; int _tmain(int argc, _TCHAR* argv[]) { // a class declaration with a template class my_temp< myclass > mytempclass; // a class declaration with a default of template my_temp<> mytempclass2; } |
Here is a full Windows VCL Application C++ Builder example of a template parameter with a default.
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 |
#include <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; // Type Name Parameter for Class Template // class name as a type parameter for a template template<class T = void> class my_temp { // template features here }; // a simple class declaration class myclass { }; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // a class declaration with a template class my_temp< myclass > mytempclass; // a class declaration with a default of template my_temp<> mytempclass2; } |
Is there a C++ FMX example of a type template parameter with a default?
Here is a full C++ FMX console example of a template parameter with a default in C++ Builder.
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 |
#include <fmx.h> // Type Name Parameter for Class Template // class name as a type parameter for a template template<class T = void> class my_temp { // template features here }; // a simple class declaration class myclass { }; int _tmain(int argc, _TCHAR* argv[]) { // a class declaration with a template class my_temp< myclass > mytempclass; // a class declaration with a default of template my_temp<> mytempclass2; } |
Here is a full Windows FMX Application C++ Builder example of a C++ template parameter with a default.
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 |
#include <fmx.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; // Type Name Parameter for Class Template // class name as a type parameter for a template template<class T = void> class my_temp { // template features here }; // a simple class declaration class myclass { }; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // a class declaration with a template class my_temp< myclass > mytempclass; // a class declaration with a default of template my_temp<> mytempclass2; } |
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