Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      typedef specifier

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General 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
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      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
       
      Declarations
       
      • typedef - creates an alias that can be used anywhere in place of a (possibly complex) type name.

      Contents

      [edit]Explanation

      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

      [edit]typedef name for linkage purposes

      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)

      [edit]Notes

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

      (since C++11)

      [edit]Keywords

      typedef

      [edit]Example

      // 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;

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 576C++98typedef was not allowed in the entire function definitionallowed in function body
      CWG 2071C++98typedef could appear in a declaration that does not contain a declaratornow disallowed

      [edit]See also

      C documentation forTypedef declaration
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/typedef&oldid=183158"

      [8]ページ先頭

      ©2009-2025 Movatter.jp