Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The typedef specifier

Reference for C++ typedef: how it creates type aliases, why function pointer and array typedefs are hard to read, and when to use using instead.

typedef declares a new name for an existing type. It comes from C and is still valid in every C++ standard, so you will meet it constantly in older code, in C headers and in code that must compile as both C and C++.

The syntax is the odd part: a typedef declaration looks exactly like a variable declaration with the word typedef in front, and the name you declare becomes the alias. That makes pointer, array and function types awkward to read. Modern C++ code prefers using Name = type;, which does the same job in a clearer order and can also be templated.

C++
typedef unsigned long ulong;       // ulong is now another name for unsigned long
typedef int (*Compare)(int, int);  // Compare = pointer to function (int, int) -> int

Try it

C++ typedef versus using

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ The typedef struct idiom and the pointer trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
typedef decl-specifiers declarator;      // declarator's name becomes the alias
typedef struct { int x; } Name;          // alias for an unnamed struct
Part Description
typedef A storage-class-like specifier. It turns the declaration into a type alias instead of declaring an object or function.
Declarator May include *, &, [], () exactly as in an ordinary declaration. The declared name is the alias.

Usage notes

  • A typedef never creates a new type, only another name. Overloading on ulong versus unsigned long is therefore impossible.
  • typedef cannot be a template. For a family of aliases, use an alias template: template <class T> using Vec = std::vector<T>;.
  • A typedef of a pointer type captures the whole pointer: const IntPtr means int* const, not const int*. This surprises people who think of a typedef as text substitution.
  • typedef int* IntPtr; followed by IntPtr a, b; makes both pointers. The raw form int* a, b; makes only a a pointer.
  • typedef struct S { ... } S; is a C habit. In C++ the tag name is already usable as a type name, so write struct S { ... };.
  • A typedef for an unnamed class gives that class a "name for linkage purposes". C++20 restricts this to classes without members that need linkage, so avoid it in new code.
  • The standard library defines nested typedefs on containers and iterators (value_type, size_type, iterator). They are normally written with using inside the class today.
  • Use typedef in headers shared with C. Everywhere else, prefer using.