C++ language is a very strong programming language from all aspects of professional and modern programming techniques. One of the great features of modern C++ is templates. With the increased use of parameterized types in C++, the need for parameterized alias template has been added since C++11. In this article, we will explain how to use alias templates with C++ examples that can be used by a professional C++ Tool.
With the increased use of parameterized types in C++, the need for parameterized type aliases has grown. This feature has been added to the C++11 standard and continued in subsequent standards. There are two main alias usage, these aliases are Type Alias and Alias Template. Let’s explain both Type Alias and Alias Template in C++.
Let’s remember how to use type alias in C++
Type Alias is a term that refers to a previously defined types, alias declaration can be used to declare a name to use as a synonym for a previously declared type. We use using
declaration (using-declaration) to declare a type alias, and it is effectively the same as typedef
. This can be in block scope, class scope, or namespace scope. Type alias does not introduce a new type and it cannot change the usage or meaning of an existing type name. A type alias declaration is completely the same as typedef declaration. Type alias can be used to create an alias template that can be used as a custom allocator.
Type aliases can help make your code more readable and also reduce the length of very long type names into something more manageable.
Here is the syntax for the type alias,
1 2 3 |
using new_identifier = type_id; |
For example, we may define ULINT type for the unsigned long int
type as below.
1 2 3 4 5 |
using ULINT = unsigned long int; ULINT a = 123456789; |
Is there an example of how to use alias template in C++?
Type alias which comes after C++11 standard, is used to create an alias template which can be used as a custom allocator.
An alias template is an alias that uses a template which refers to a family of types.
For example, let’s create a table template which has type
, rows
and cols
parameters. We can create this table (my_table
) template as below,
1 2 3 4 5 6 |
// A Template Example template <typename T, int rows, int cols> class my_table { }; |
We can use this template to create two more alias templates. Here we create a single column (my_column
) and a single row (my_row
) templates as below,
1 2 3 4 5 |
// Alias Template Example template <typename T, int rows> using my_column = my_table<T, rows, 1>; template <typename T, int cols> using my_row = my_table<T, 1, cols>; |
As you see, we have a my_table
template and my_column
, my_row
templates which are alias templates of the my_table
template. These templates can be used to with any data types (int
, float
, char
, string
, wstring
, etc.). Now we can use all the templates to create a table which has rows
and columns
or a single row or a single column data in a given type. Here is how we can use them,
1 2 3 4 5 |
my_table<int, 50, 10> table1; my_column<int, 20> col1; my_row<int, 80> row1; |
Is there a full example to how to use alias template in C++?
Here is a full example of an alias template in C++.
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 |
#include <iostream> // A Template Example template <typename T, int rows, int cols> class my_table { }; // Alias Template Examples template <typename T, int rows> using my_column = my_table<T, rows, 1>; template <typename T, int cols> using my_row = my_table<T, 1, cols>; int main() { my_table<char, 20, 10> table1; my_table<int, 50, 10> table2; my_table<float, 200, 100> table3; my_column<char, 10> col1; my_column<int, 50> col2; my_column<float, 100> col3; my_row<char, 20> row1; my_row<int, 80> row2; my_row<float, 400> row3; return 0; } |
For more information about alias template feature, please see Alias templates Proposal document.
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 rom here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition