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.
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
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.
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
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
ulongversusunsigned longis therefore impossible. typedefcannot 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 IntPtrmeansint* const, notconst int*. This surprises people who think of a typedef as text substitution. typedef int* IntPtr;followed byIntPtr a, b;makes both pointers. The raw formint* a, b;makes onlyaa pointer.typedef struct S { ... } S;is a C habit. In C++ the tag name is already usable as a type name, so writestruct 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 withusinginside the class today. - Use
typedefin headers shared with C. Everywhere else, preferusing.
Related
- using
- struct and class
- Pointers
- Tutorial: Type aliases, Function pointers