typedef
specifierGeneral topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
|
typedef
- creates an alias that can be used anywhere in place of a (possibly complex) type name.Contents |
Thetypedef specifier, when used in adeclaration, specifies that the declaration is atypedef declaration rather than a variable or function declaration.
Typically, thetypedef specifier appears at the start of the declaration, though it is permitted to appear after thetype specifiers, or between two type specifiers. Thetypedef 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 toint), it may declare array and function types, pointers and references, class types, etc. Every identifier introduced in this declaration becomes atypedef name, which is a synonym for the type of the object or function that it would become if the keywordtypedef 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.
Thetypedef specifier may not appear in the declaration of a function parameter nor in thedecl-specifier-seq of afunction definition:
void f1(typedefint param);// ill-formedtypedefint f2(){}// ill-formed
Thetypedef specifier may not appear in a declaration that does not contain a declarator:
typedefstruct X{};// ill-formed
If a typedef declaration defines an unnamedclass orenumeration, the first typedef name of the class type or enumeration type declared by the declaration is thetypedef name for linkage purposes of that type.
For example, intypedefstruct{/* ... */} S;,S
is a typedef name for linkage purposes. The class or enumeration type defined in this way hasexternal 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
and all member classes must also satisfy these requirements (recursively). | (since C++20) |
Type aliases provide the same functionality as typedef declarations using a different syntax, and are also applicable to template names. | (since C++11) |
// simple typedeftypedefunsignedlong ulong; // the following two objects have the same typeunsignedlong l1;ulong l2; // more complicated typedeftypedefint int_t,*intp_t,(&fp)(int, ulong), arr_t[10]; // the following two objects have the same typeint a1[10];arr_t a2; // beware: the following two objects do not have the same typeconst intp_t p1=0;// int *const p1 = 0constint*p2; // common C idiom to avoid having to write "struct S"typedefstruct{int a;int b;} S,*pS; // the following two objects have the same typepS ps1;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-seqlongunsignedtypedefintlong ullong;// more conventionally spelled "typedef unsigned long long int ullong;" // std::add_const, like many other metafunctions, use member typedefstemplate<class T>struct add_const{typedefconst T type;}; typedefstruct Node{struct listNode* 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{void f(){}} C_Incompatible;
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 |
C documentation forTypedef declaration |