Type Aliases: using & typedef in C++
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.
Name intent, not storage
using UserId = int; turns bare ints into self-documenting identity values at zero cost — aliases vanish after compilation.
using beats typedef
using Name = Type; reads naturally left to right; typedef Type Name; reverses the order and cannot express template aliases.
Alias templates
template <typename T> using Matrix = std::vector<std::vector<T>>; parameterizes the alias itself — impossible with typedef.
Shorten nested names
using Row = std::vector<std::string>; collapses verbose STL spellings that would otherwise sprawl across signatures.
#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;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.