Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Fundamental types

      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
       
       

      (See alsotype for type system overview andthe list of type-related utilities that are provided by the C++ library)

      The following types are collectively calledfundamental types :

      • (possibly cv-qualified)void
      (since C++11)

      Contents

      [edit]void

      void — type with an empty set of values. It is anincomplete type that cannot be completed (consequently, objects of typevoid are disallowed). There are noarrays ofvoid, norreferences tovoid. However,pointers tovoid andfunctions returning typevoid (procedures in other languages) are permitted.

      std::nullptr_t

      Defined in header<cstddef>
      typedef decltype(nullptr) nullptr_t;
      (since C++11)

      std::nullptr_t is the type of the null pointer literal,nullptr. It is a distinct type that is not itself a pointer type or a pointer to member type. All Its prvalues arenull pointer constants.

      sizeof(std::nullptr_t) is equal tosizeof(void*).

      (since C++11)

      [edit]Integral types

      [edit]Standard integer types

      int — basic integer type. The keywordint may be omitted if any of the modifiers listed below are used. If no length modifiers are present, it's guaranteed to have a width of at least 16 bits. However, on 32/64 bit systems it is almost exclusively guaranteed to have width of at least 32 bits (see below).
      [edit]Modifiers

      Modifies the basic integer type. Can be mixed in any order. Only one of each group can be present in type name.

      • Signedness:
      signed — target type will have signed representation (this is the default if omitted)
      unsigned — target type will have unsigned representation
      • Size:
      short — target type will be optimized for space and will have width of at least 16 bits.
      long — target type will have width of at least 32 bits.

      longlong — target type will have width of at least 64 bits.
      (since C++11)

      Note: as with all type specifiers, any order is permitted:unsignedlonglongint andlongintunsignedlong name the same type.

      [edit]Properties

      The following table summarizes all available standard integer types and their properties in various common data models:

      Type specifierEquivalent typeWidth in bits bydata model
      C++ standard LP32 ILP32 LLP64 LP64
      signedchar
      signedcharat least
      8
      8888
      unsignedchar
      unsignedchar
      short
      shortintat least
      16
      16161616
      shortint
      signedshort
      signedshortint
      unsignedshort
      unsignedshortint
      unsignedshortint
      int
      intat least
      16
      16323232
      signed
      signedint
      unsigned
      unsignedint
      unsignedint
      long
      longintat least
      32
      32323264
      longint
      signedlong
      signedlongint
      unsignedlong
      unsignedlongint
      unsignedlongint
      longlong
      longlongint
      (C++11)
      at least
      64
      64646464
      longlongint
      signedlonglong
      signedlonglongint
      unsignedlonglong
      unsignedlonglongint
      (C++11)
      unsignedlonglongint

      Note: integer arithmetic is defined differently for the signed and unsigned integer types. Seearithmetic operators, in particularinteger overflows.

      std::size_t is the unsigned integer type of the result of thesizeof operator as well as thesizeof... operator and thealignof operator(since C++11).

      Extended integer types

      The extended integer types are implementation-defined. Note thatfixed width integer types are typically aliases of the standard integer types.

      (since C++11)

      [edit]Boolean type

      bool — integer type, capable of holding one of the two values:true orfalse. The value ofsizeof(bool) is implementation defined and might differ from1.

      [edit]Character types

      Character types are integer types used for a character representation.

      signedchar — type for signed character representation.
      unsignedchar — type for unsigned character representation. Also used to inspectobject representations (raw memory).
      char — type for character representation which can be most efficiently processed on the target system (has the same representation and alignment as eithersignedchar orunsignedchar, but is always a distinct type).Multibyte characters strings use this type to represent code units.For every value of typeunsignedchar in range[0255], converting the value tochar and then back tounsignedchar produces the original value.(since C++11) The signedness ofchar depends on the compiler and the target platform: the defaults for ARM and PowerPC are typically unsigned, the defaults for x86 and x64 are typically signed.
      wchar_t — type for wide character representation (seewide strings). It has the same size, signedness, and alignment as one of the integer types, but is a distinct type. In practice, it is 32 bits and holds UTF-32 on Linux and many other non-Windows systems, but 16 bits and holds UTF-16 code units on Windows. The standard used to requirewchar_t to be large enough to represent any supported character code point. However, such requirement cannot be fulfilled on Windows, and thus it is considered as adefect and removed.

      char16_t — type for UTF-16 character representation, required to be large enough to represent any UTF-16 code unit (16 bits). It has the same size, signedness, and alignment asstd::uint_least16_t, but is a distinct type.

      char32_t — type for UTF-32 character representation, required to be large enough to represent any UTF-32 code unit (32 bits). It has the same size, signedness, and alignment asstd::uint_least32_t, but is a distinct type.
      (since C++11)

      char8_t — type for UTF-8 character representation, required to be large enough to represent any UTF-8 code unit (8 bits). It has the same size, signedness, and alignment asunsignedchar (and therefore, the same size and alignment aschar andsignedchar), but is a distinct type.
      (since C++20)

      Besides the minimal bit counts, the C++ Standard guarantees that

      1==sizeof(char)sizeof(short)sizeof(int)sizeof(long)sizeof(longlong).

      Note: this allows the extreme case in whichbytes are sized 64 bits, all types (includingchar) are 64 bits wide, andsizeof returns1 for every type.

      [edit]Floating-point types

      [edit]Standard floating-point types

      The following three types and their cv-qualified versions are collectively called standard floating-point types.

      float — single precision floating-point type. UsuallyIEEE-754 binary32 format.
      double — double precision floating-point type. UsuallyIEEE-754 binary64 format.
      longdouble — extended precision floating-point type. Does not necessarily map to types mandated by IEEE-754.

      Extended floating-point types

      The extended floating-point types are implementation-defined. They may includefixed width floating-point types.

      (since C++23)

      [edit]Properties

      Floating-point types may supportspecial values:

      • infinity (positive and negative), seeINFINITY
      • thenegative zero,-0.0. It compares equal to the positive zero, but is meaningful in some arithmetic operations, e.g.1.0/0.0==INFINITY, but1.0/-0.0==-INFINITY), and for some mathematical functions, e.g.sqrt(std::complex)
      • not-a-number (NaN), which does not compare equal with anything (including itself). Multiple bit patterns represent NaNs, seestd::nan,NAN. Note that C++ takes no special notice of signalling NaNs other than detecting their support bystd::numeric_limits::has_signaling_NaN, and treats all NaNs as quiet.

      Floating-point numbers may be used witharithmetic operators+,-,/, and* as well as various mathematical functions from<cmath>. Both built-in operators and library functions may raise floating-point exceptions and seterrno as described inmath errhandling.

      Floating-point expressions may have greater range and precision than indicated by their types, seeFLT_EVAL_METHOD. Floating-point expressions may also becontracted, that is, calculated as if all intermediate values have infinite range and precision, see#pragma STDC FP_CONTRACT. Standard C++ does not restrict the accuracy of floating-point operations.

      Some operations on floating-point numbers are affected by and modify the state ofthe floating-point environment (most notably, the rounding direction).

      Implicit conversions are defined between floating types and integer types.

      Seelimits of floating-point types andstd::numeric_limits for additional details, limits, and properties of the floating-point types.

      [edit]Range of values

      The following table provides a reference for the limits of common numeric representations.

      Prior to C++20, the C++ Standard allowed any signed integer representation, and the minimum guaranteed range of N-bit signed integers was from\(\scriptsize -(2^{N-1}-1)\)-(2N-1
      -1)
      to\(\scriptsize +2^{N-1}-1\)+2N-1
      -1
      (e.g.−127 to127 for a signed 8-bit type), which corresponds to the limits ofones' complement orsign-and-magnitude.

      However, all C++ compilers usetwo's complement representation, and as of C++20, it is the only representation allowed by the standard, with the guaranteed range from\(\scriptsize -2^{N-1}\)-2N-1
      to\(\scriptsize +2^{N-1}-1\)+2N-1
      -1
      (e.g.−128 to127 for a signed 8-bit type).

      8-bit ones' complement and sign-and-magnitude representations forchar have been disallowed since C++11 (via the resolution ofCWG issue 1759), because a UTF-8 code unit of value 0x80 used in aUTF-8 string literal must be storable in achar type object.

      The range for a floating-point typeT is defined as follows:

      • The minimum guaranteed range is the most negative finite floating-point number representable inT through the most positive finite floating-point number representable inT.
      • If negative infinity is representable inT, the range ofT is extended to all negative real numbers.
      • If positive infinity is representable inT, the range ofT is extended to all positive real numbers.

      Since negative and positive infinity are representable inISO/IEC/IEEE 60559 formats, all real numbers lie within the range of representable values of a floating-point type adhering to ISO/IEC/IEEE 60559.

      TypeSize in bitsFormatValue range
      ApproximateExact
      character8signed−128 to127
      unsigned0 to255
      16UTF-160 to65535
      32UTF-320 to1114111 (0x10ffff)
      integer16signed± 3.27 · 104−32768 to32767
      unsigned0 to6.55 · 1040 to65535
      32signed± 2.14 · 109−2,147,483,648 to2,147,483,647
      unsigned0 to4.29 · 1090 to4,294,967,295
      64signed± 9.22 · 1018−9,223,372,036,854,775,808 to9,223,372,036,854,775,807
      unsigned0 to1.84 · 10190 to18,446,744,073,709,551,615
      binary
      floating-
      point
      32IEEE-754
      • min subnormal:
        ± 1.401,298,4 · 10−45
      • min normal:
        ± 1.175,494,3 · 10−38
      • max:
        ± 3.402,823,4 · 1038
      • min subnormal:
        ±0x1p−149
      • min normal:
        ±0x1p−126
      • max:
        ±0x1.fffffep+127
      64IEEE-754
      • min subnormal:
        ± 4.940,656,458,412 · 10−324
      • min normal:
        ± 2.225,073,858,507,201,4 · 10−308
      • max:
        ± 1.797,693,134,862,315,7 · 10308
      • min subnormal:
        ±0x1p−1074
      • min normal:
        ±0x1p−1022
      • max:
        ±0x1.fffffffffffffp+1023
      80[note 1]x86
      • min subnormal:
        ± 3.645,199,531,882,474,602,528
         · 10−4951
      • min normal:
        ± 3.362,103,143,112,093,506,263
         · 10−4932
      • max:
        ± 1.189,731,495,357,231,765,021
         · 104932
      • min subnormal:
        ±0x1p−16445
      • min normal:
        ±0x1p−16382
      • max:
        ±0x1.fffffffffffffffep+16383
      128IEEE-754
      • min subnormal:
        ± 6.475,175,119,438,025,110,924,
        438,958,227,646,552,5 · 10−4966
      • min normal:
        ± 3.362,103,143,112,093,506,262,
        677,817,321,752,602,6 · 10−4932
      • max:
        ± 1.189,731,495,357,231,765,085,
        759,326,628,007,016,2 · 104932
      • min subnormal:
        ±0x1p−16494
      • min normal:
        ±0x1p−16382
      • max:
        ±0x1.ffffffffffffffffffffffffffff
        p+16383
      1. The object representation usually occupies 96/128 bits on 32/64-bit platforms respectively.

      Note: actual (as opposed to guaranteed minimal) limits on the values representable by these types are available inC numeric limits interface andstd::numeric_limits.

      [edit]Data models

      The choices made by each implementation about the sizes of the fundamental types are collectively known asdata model. Four data models found wide acceptance:

      32 bit systems:

      • LP32 or2/4/4 (int is 16-bit,long and pointer are 32-bit)
      • Win16 API
      • ILP32 or4/4/4 (int,long, and pointer are 32-bit);
      • Win32 API
      • Unix and Unix-like systems (Linux, macOS)

      64 bit systems:

      • LLP64 or4/4/8 (int andlong are 32-bit, pointer is 64-bit)
      • LP64 or4/8/8 (int is 32-bit,long and pointer are 64-bit)
      • Unix and Unix-like systems (Linux, macOS)

      Other models are very rare. For example,ILP64 (8/8/8:int,long, and pointer are 64-bit) only appeared in some early 64-bit Unix systems (e.g.UNICOS on Cray).

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_unicode_characters200704L(C++11)New character types (char16_t andchar32_t)
      __cpp_char8_t201811L(C++20)char8_t
      202207L(C++23)char8_t compatibility and portability fix (allow initialization of(unsigned) char arrays fromUTF-8 string literals)

      [edit]Keywords

      void,bool,true,false,char,char8_t,char16_t,char32_t,wchar_t,int,short,long,signed,unsigned,float,double

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 238C++98the constraints placed on a floating-point implementation was unspecifiedspecified as
      no constraint
      CWG 1759C++11char is not guaranteed to be able to represent UTF-8 code unit 0x80guaranteed
      CWG 2689C++11cv-qualifiedstd::nullptr_t was not a fundemental typeit is
      CWG 2723C++98the ranges of representable values for floating-point types were not specifiedspecified
      P2460R2C++98wchar_t was required to be able to represent distinct codes for all members
      of the largest extended character set specified among the supported locales
      not required

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 6.8.2 Fundamental types [basic.fundamental]
      • C++20 standard (ISO/IEC 14882:2020):
      • 6.8.1 Fundamental types [basic.fundamental]
      • C++17 standard (ISO/IEC 14882:2017):
      • 6.9.1 Fundamental types [basic.fundamental]
      • C++14 standard (ISO/IEC 14882:2014):
      • 3.9.1 Fundamental types [basic.fundamental]
      • C++11 standard (ISO/IEC 14882:2011):
      • 3.9.1 Fundamental types [basic.fundamental]
      • C++03 standard (ISO/IEC 14882:2003):
      • 3.9.1 Fundamental types [basic.fundamental]
      • C++98 standard (ISO/IEC 14882:1998):
      • 3.9.1 Fundamental types [basic.fundamental]

      [edit]See also

      C documentation forarithmetic types

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/types&oldid=180189"

      [8]ページ先頭

      ©2009-2025 Movatter.jp