[dcl.typedef] (original) (raw)
9 Declarations [dcl.dcl]
9.2 Specifiers [dcl.spec]
9.2.4 The typedef specifier [dcl.typedef]
Declarations containing the decl-specifier typedefdeclare identifiers that can be used later for namingfundamental or compoundtypes.
If a typedef specifier appears in a declaration without a declarator, the program is ill-formed.
A name declared with the typedef specifier becomes atypedef-name.
Atypedef-name does not introduce a new type the way a class declaration ([class.name]) or enum declaration ([dcl.enum]) does.
[Example 1:
Aftertypedef int MILES, *KLICKSP;the constructionsMILES distance;extern KLICKSP metricp;are all correct declarations; the type of distance isint and that of metricp is “pointer to int”.
— _end example_]
Such a typedef-name has the same semantics as if it were introduced by the typedef specifier.
In particular, it does not define a new type.
[Example 2: using handler_t = void (*)(int);extern handler_t ignore;extern void (*ignore)(int); using cell = pair<void*, cell*>; — _end example_]
In a given non-class scope, a typedef specifier can be used to redeclare the name of any type declared in that scope to refer to the type to which it already refers.
[Example 3: typedef struct s { } s;typedef int I;typedef int I;typedef I I; — _end example_]
In a given class scope, a typedef specifier can be used to redeclare any class-name declared in that scope that is not also a typedef-name to refer to the type to which it already refers.
[Example 4: struct S { typedef struct A { } A; typedef struct B B; typedef A A; }; — _end example_]
If a typedef specifier is used to redeclare in a given scope an entity that can be referenced using an elaborated-type-specifier, the entity can continue to be referenced by anelaborated-type-specifier or as an enumeration or class name in an enumeration or class definition respectively.
[Example 5: struct S;typedef struct S S;int main() { struct S* p; } struct S { }; — _end example_]
In a given scope, a typedef specifier shall not be used to redeclare the name of any type declared in that scope to refer to a different type.
[Example 6: class complex { };typedef int complex; — _end example_]
Similarly, in a given scope, a class or enumeration shall not be declared with the same name as a typedef-name that is declared in that scope and refers to a type other than the class or enumeration itself.
[Example 7: typedef int complex;class complex { }; — _end example_]
[Example 8: struct S { S();~S();};typedef struct S T; S a = T(); struct T * p; — _end example_]
If the typedef declaration defines an unnamed class or enumeration, the firsttypedef-name declared by the declaration to be that type is used to denote the type for linkage purposes only ([basic.link]).
[Note 2:
A typedef declaration involving a lambda-expressiondoes not itself define the associated closure type, and so the closure type is not given a name for linkage purposes.
— _end note_]
[Example 9: typedef struct { } *ps, S; typedef decltype([]{}) C; — _end example_]
An unnamed class with a typedef name for linkage purposes shall 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 shall also satisfy these requirements (recursively).
[Example 10: typedef struct { int f() {} } X; — _end example_]