Table of Contents
The using keyword can also be used to create a type alias. It provides the same functionality as typedef but offers a more consistent syntax and is especially useful when working with templates.
This example demonstrates how to define and use a type alias with using.
Source Code #
#include <iostream>
using uint = unsigned int;
int main()
{
uint student_count = 120;
std::cout << "Student Count: " << student_count << '\n';
return 0;
}
Output #
Student Count: 120
Explanation #
using uint = unsigned int;creates the aliasuintfor the existing typeunsigned int.- The
usingkeyword does not define a new data type. It only introduces another name for an existing type. uint student_count = 120;is equivalent to writingunsigned int student_count = 120;.- Compared to
typedef, theusingsyntax is easier to read because the alias name appears on the left side of the assignment. usingis the preferred approach for creating type aliases in modern C++ and also supports template aliases.std::cout << "Student Count: " << student_count << '\n';prints the value stored in the variable.return 0;terminates the program successfully.