typedef specifier - cppreference.com

From cppreference.com

typedef - creates an alias that can be used anywhere in place of a (possibly complex) type name.

Explanation

The typedef specifier, when used in a

declaration

, specifies that the declaration is a typedef declaration rather than a variable or function declaration.

Typically, the typedef specifier appears at the start of the declaration, though it is permitted to appear after the

type specifiers

, or between two type specifiers. The typedef specifier cannot be combined with any other specifier except for type specifiers.

A typedef declaration may declare one or many identifiers on the same line (e.g. int and a pointer to int), it may declare array and function types, pointers and references, class types, etc. Every identifier introduced in this declaration becomes a typedef name, which is a synonym for the type of the object or function that it would become if the keyword typedef were removed.

The typedef names are aliases for existing types, and are not declarations of new types. typedef cannot be used to change the meaning of an existing type name (including a typedef name). Once declared, a typedef name may only be redeclared to refer to the same type again. Typedef names are only in effect in the scope where they are visible: different functions or class declarations may define identically-named types with different meaning.

The typedef specifier may not appear in the declaration of a function parameter nor in the decl-specifier-seq of a

function definition

:

voidf1(typedefintparam);// ill-formedtypedefintf2(){}// ill-formedThe typedef specifier may not appear in a declaration that does not contain a declarator:

typedefstructX{};// ill-formedtypedef name for linkage purposes

If a typedef declaration defines an unnamed

class

or

enumeration

, the first typedef name of the class type or enumeration type declared by the declaration is the typedef name for linkage purposes of that type.

For example, in typedefstruct{/* ... */}S;, S is a typedef name for linkage purposes. The class or enumeration type defined in this way has

external linkage

(unless it is in an unnamed namespace).

An unnamed class defined in this way should only contain C-compatible constructs. In particular, it must not

declare any members other than non-static data members, member enumerations, or member classes,

have any

base classes

or

default member initializers

, or

contain a

lambda expression

,

and all member classes must also satisfy these requirements (recursively).

(since C++20)Notes

Type aliases

provide the same functionality as typedef declarations using a different syntax, and are also applicable to template names.

(since C++11)Keywords

typedef

Example

// simple typedeftypedefunsignedlongulong;// the following two objects have the same typeunsignedlongl1;ulongl2;// more complicated typedeftypedefintint_t,*intp_t,(&fp)(int,ulong),arr_t[10];// the following two objects have the same typeinta1[10];arr_ta2;// beware: the following two objects do not have the same typeconstintp_tp1=0;// int *const p1 = 0constint*p2;// common C idiom to avoid having to write "struct S"typedefstruct{inta;intb;}S,*pS;// the following two objects have the same typepSps1;S*ps2;// error: storage-class-specifier cannot appear in a typedef declaration// typedef static unsigned int uint;// typedef can be used anywhere in the decl-specifier-seqlongunsignedtypedefintlongullong;// more conventionally spelled "typedef unsigned long long int ullong;"// std::add_const, like many other metafunctions, use member typedefstemplate<classT>structadd_const{typedefconstTtype;};typedefstructNode{structlistNode*next;// declares a new (incomplete) struct type named listNode}listNode;// error: conflicts with the previously declared struct name// C++20 error: "struct with typedef name for linkage" has member functionstypedefstruct{voidf(){}}C_Incompatible;Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

CWG 576

C++98 typedef was not allowed in the entire function definition allowed in function body

CWG 2071

C++98 typedef could appear in a declaration that does not contain a declarator now disallowed See also

Type alias

Alias template