C++C++11C++14C++17C++20Learn C++SyntaxTemplates

What Are The Type Template Parameters In C++

What Are The Type Template Parameters In C++

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. 

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:

We can declare a template as below:

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’:

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’:

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’:

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:

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,

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.

What Are The Type Template Parameters In C++ the C++ Builder Logo

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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?