Site icon Learn C++

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.

[crayon-6635773bdf049188307293/]

The parameters of a template can be,

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.

For example, we can declare a template as below.

[crayon-6635773bdf051325019354/]

How can we use typename in C++?

Here is a typename usage in template syntax.

[crayon-6635773bdf053174051682/]

In general, a typename can be used as follows.

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.

[crayon-6635773bdf055280074102/]

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.

[crayon-6635773bdf057677085797/]

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

[crayon-6635773bdf059402646025/]

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:

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

Exit mobile version