Learn C++
#include "variablesdatatypes/type_aliases.hpp"

Type Aliases: using & typedef in C++

//concept

Type aliases give long or meaningful types short names: using UserId = int; lets code say UserId instead of int wherever user identity flows. Unlike copy-paste, an alias documents intent — a reviewer instantly knows what the value means — and changing the underlying type later touches one line instead of hundreds.

The modern using form reads left-to-right and works with templates (template <typename T> using Matrix = std::vector<std::vector<T>>;), while legacy typedef reads inside-out and cannot alias templates at all. New code should use using exclusively; typedef survives only in old headers.

//how it works
1

Name intent, not storage

using UserId = int; turns bare ints into self-documenting identity values at zero cost — aliases vanish after compilation.

2

using beats typedef

using Name = Type; reads naturally left to right; typedef Type Name; reverses the order and cannot express template aliases.

3

Alias templates

template <typename T> using Matrix = std::vector<std::vector<T>>; parameterizes the alias itself — impossible with typedef.

4

Shorten nested names

using Row = std::vector<std::string>; collapses verbose STL spellings that would otherwise sprawl across signatures.

//example
#include <iostream>
#include <string>
 
using UserId = int;
using Name = std::string;
 
int main() {
UserId id = 7;
Name who = "ada";
std::cout << who << "#" << id << std::endl;
return 0;
}
Output
ada#7
1UserId and Name alias int and std::string — the declarations read as domain concepts, not storage types.
2Variables declared with aliases behave exactly like their underlying types in every way.
3ada#7 prints — aliases changed readability, nothing else.
//use cases
Domain vocabularies (UserId, Meters, Price) that prevent unit mix-ups
Shortening nested STL types in signatures and member declarations
Template aliases for parameterized containers and policies
Migrating legacy typedefs to readable using declarations

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself — building beats watching.