Programming
C++
Typedef
Using Keyword
Code Comparison

What is the difference between 'typedef' and 'using'?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

typedef and using can both create a type alias in C++, so for simple cases they seem interchangeable. The practical difference is that using has clearer syntax and supports alias templates directly, which makes it the more flexible choice in modern C++.

Neither keyword creates a brand-new type. Both simply give another name to an existing type, which is an important distinction when you think about overloads, conversions, and readability.

Simple Aliases: Both Work

For a basic alias, the two forms are equivalent:

cpp
1#include <iostream>
2
3typedef unsigned long IndexA;
4using IndexB = unsigned long;
5
6int main() {
7    IndexA a = 10;
8    IndexB b = 20;
9    std::cout << a + b << '\n';
10}

Both IndexA and IndexB are just aliases for unsigned long.

For simple aliases, the real difference is readability. Many developers find using Name = Type; easier to read because the alias name appears on the left and the aliased type appears on the right, like a normal assignment-style declaration.

Complex Types Read Better With using

This difference becomes clearer with function pointers:

cpp
typedef void (*OldStyleHandler)(int);
using NewStyleHandler = void (*)(int);

Both are valid, but using is usually easier to parse quickly.

That readability benefit scales up as the aliased type becomes more complicated. For modern code, that is one of the biggest practical arguments for preferring using.

Alias Templates Are the Big Functional Advantage

typedef cannot create an alias template directly. using can:

cpp
1#include <vector>
2
3template <typename T>
4using Vec = std::vector<T>;
5
6int main() {
7    Vec<int> values = {1, 2, 3};
8}

With typedef, you need an extra wrapper struct:

cpp
1#include <vector>
2
3template <typename T>
4struct VecWrapper {
5    typedef std::vector<T> type;
6};
7
8int main() {
9    VecWrapper<int>::type values = {1, 2, 3};
10}

The wrapper works, but it is noisier and less direct. This is the main reason using is considered more powerful in template-heavy C++.

using Is Not Only for Type Aliases

One reason the keyword can feel confusing is that using does more than alias types. In other contexts it is also used for:

  • namespace imports such as using std::cout;
  • inherited constructors
  • bringing base-class members into scope

But in an alias declaration, the pattern is specifically:

cpp
using Name = ExistingType;

That syntax has a single clear meaning: create an alias.

Neither One Creates a Distinct Type

This is a common misunderstanding:

cpp
using UserId = int;
using ProductId = int;

Even though the names suggest different meanings, both aliases are still just int. The compiler does not treat them as separate strong types.

So this works without conversion:

cpp
1#include <iostream>
2
3using UserId = int;
4using ProductId = int;
5
6void printValue(int x) {
7    std::cout << x << '\n';
8}
9
10int main() {
11    UserId user = 5;
12    ProductId product = 9;
13    printValue(user);
14    printValue(product);
15}

If you need real type safety, use a wrapper class or struct instead of an alias.

When typedef Still Appears

You will still see typedef in:

  • older C and C++ codebases
  • C headers shared with C++
  • legacy documentation and interview questions

There is nothing wrong with understanding or maintaining it. But in new C++ code, using is usually preferred because it is more consistent with modern style and works better with templates.

Common Pitfalls

  • Assuming typedef and using create distinct new types.
  • Preferring typedef in template-heavy code where alias templates would be cleaner.
  • Mixing up using alias declarations with other using forms such as namespace imports.
  • Rewriting old typedef statements mechanically without checking whether readability actually improves.
  • Using aliases where a real wrapper type would be safer and clearer.

Summary

  • 'typedef and using both create type aliases.'
  • 'using is usually easier to read, especially for complex types.'
  • 'using supports alias templates directly, while typedef does not.'
  • Neither keyword creates a new strong type; both only create alternate names.
  • In modern C++, using is usually the better default unless you are working in legacy-style code.

Course illustration
Course illustration

All Rights Reserved.