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.
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 |
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.
1 2 3 4 5 |
template<typename T> typename my_temp { }; |
How can we use typename in C++?
Here is a typename
usage in template syntax.
1 2 3 |
template< typename name(optional) = default > declaration |
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.
1 2 3 4 5 6 7 8 9 10 11 |
template<typename T> class my_temp { T a; }; int main() { class my_temp<char> o1; // private "T a" is now "char a" } |
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.
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 |
#include <iostream> template<typename T> class my_temp { private: T a; public: void print_size() { std::cout << sizeof(a) << " bytes" <<std::endl; } }; int main() { class my_temp<char> o1; // private "T a" is now "char a" class my_temp<short int> o2; // private "T a" is now "short int a" class my_temp<int> o3; // private "T a" is now "int a" o1.print_size(); o2.print_size(); o3.print_size(); system("pause"); return 0; } |
Here is the output of each typenames that we give there (char
, short int
, and int
),
1 2 3 4 5 6 |
1 bytes 2 bytes 4 bytes Press any key to continue . . . |
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
, ormy_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.
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