C++ language is a very strong programming language for 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 type alias was added since C++11. In this article, we will explain how to use type alias with C++ examples that can be used by a professional C++ Developer Tool.
There are two main uses of type aliases: Type Alias and Alias Template.
Let’s explain Type Alias in C++.
What does type alias mean in C++?
Type Alias is a term that refers to a previously defined type but under a different name – just like Bruce Wayne uses the alias Batman when is out fighting crime. The 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 same as the typedef command. 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 the 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 6 7 |
using ULINT = unsigned long int; ULINT a = 123456789; ULINT b = 987654321; ULINT c = 74; |
A full example of how to use type alias in C++
Here is a full C++ example about using type alias.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> int main() { using ULINT = unsigned long int; ULINT a = 123456789; ULINT b = 987654321; ULINT c = 74; return 0; } |
as you see, we just use our new declaration name ULINT
, we shorten the unsigned long int
, but its usage and the meaning of this type name has been not changed.
If you are looking how to use alias template please use this dynamic search: https://learncplusplus.org/tag/alias-template/
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 from here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition