Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

typedef — type alias

The typedef keyword gives a type an additional name without creating a distinct type. Declarator syntax also lets it name pointer, array, and function types.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

typedef type-specifier-seq declarator;
typedef int Count;
typedef int* IntPtr;
typedef int (*Transform)(int);

typedef declares a type alias: typedef int Count; makes Count name the same type as int. It does not declare a variable or provide the type distinction that a separate class would provide.

The alias name occupies the name position in a declarator. In typedef int A, *P;, A names int, while P names int*. The declaration typedef int (*Transform)(int); names a pointer-to-function type taking int and returning int.

An alias is not textual substitution. In the example, const CountPtr means a constant pointer to modifiable Count, not a pointer to constant Count. You can change *pointer, but cannot assign another address to pointer.

The declaration using Count = int; gives the alias the same meaning as typedef int Count;. The supplied source is an evolving standard draft, not a frozen C++23 publication; the example is intended for local verification in C++23 mode.

Examples

#include <iostream>

typedef int Count;
typedef Count* CountPtr;

int main() {
    Count value = 7;
    const CountPtr pointer = &value;
    *pointer += 5;
    std::cout << value << '\n';
}

Result

12

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources