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

Learn What Is Typename Or Type Name In C++

Learn What Is Typename Or Type Name In C++

The template feature in C++ is one of the great features of modern C++. 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 what is typename and how you can use them with templates in C++ that you can use in any modern, professional or free C++ IDE and compiler that supports C++11, C++14, C++17, and over standards. 

What is a template in C++?

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.

The parameters of a template can be,

  • type template parameter,
  • non-type template parameter,
  • template template parameter (a template used as a parameter).

What is typename in C++?

A typename (Type Name) is used to specify a type parameter in template parameter lists. The typename provides an unknown identifier, it is a type as a hint to the compiler in template definitions. Since C++17, a typename can be used as an alternative to a class to declare type template parameters and template template parameters.

In general, a type parameter can be one of the following types.

  • typename
  • class
  • concept (since C++20)

For example, we can declare a template as below.

How can we use typename in C++?

Here is a typename usage in template syntax.

In general, a typename can be used as follows.

  • typename can be used to declare that a dependent qualified name inside a declaration or a definition of a template.
  • Until C++11,  typename was being used as a non-dependent qualified type name, used inside a declaration or a definition of a template. This feature is not available in C++14, C++17 and above
  • Since C++17, typename can be used in the template parameter list of a template declaration as an alternative to class to declare type template parameters and template template parameters
  • Since C++20, typename can be used inside a requirement for type requirements.

Is there a simple example of how to use typename in C++?

Here is a simple example of how we can use typename in templates.

Is there a full example of how to use typename in C++?

Here is a full example that shows how we can use typename in templates.

Here is the output of each typenames that we give there (char, short int, and int),

Where can we not use typename in C++?

Note that, according to standard C++98, and C++11, there are some restrictions (source) when using typename. Here are some of these:

  • typename is prohibited in these conditions,
    • Outside of a template definition
    • Before an unqualified type, like int, float, or my_special_type.
    • When naming a base class.
    • In a constructor initialization list.
  • typename is optional in other scenarios.
  • typename is mandatory before a qualified, dependent name which refers to a type.
Learn What Is Typename Or Type Name 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
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++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?