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 | ||||||||||||||||
| ||||||||||||||||
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 :
| (since C++11) |
Contents |
std::nullptr_t
std::nullptr_t is the type of the null pointer literal, sizeof(std::nullptr_t) is equal tosizeof(void*). | (since C++11) |
Modifies the basic integer type. Can be mixed in any order. Only one of each group can be present in type name.
| (since C++11) |
Note: as with all type specifiers, any order is permitted:unsignedlonglongint andlongintunsignedlong name the same type.
The following table summarizes all available standard integer types and their properties in various common data models:
Type specifier | Equivalent type | Width in bits bydata model | ||||
---|---|---|---|---|---|---|
C++ standard | LP32 | ILP32 | LLP64 | LP64 | ||
signedchar | signedchar | at least 8 | 8 | 8 | 8 | 8 |
unsignedchar | unsignedchar | |||||
short | shortint | at least 16 | 16 | 16 | 16 | 16 |
shortint | ||||||
signedshort | ||||||
signedshortint | ||||||
unsignedshort | unsignedshortint | |||||
unsignedshortint | ||||||
int | int | at least 16 | 16 | 32 | 32 | 32 |
signed | ||||||
signedint | ||||||
unsigned | unsignedint | |||||
unsignedint | ||||||
long | longint | at least 32 | 32 | 32 | 32 | 64 |
longint | ||||||
signedlong | ||||||
signedlongint | ||||||
unsignedlong | unsignedlongint | |||||
unsignedlongint | ||||||
longlong | longlongint (C++11) | at least 64 | 64 | 64 | 64 | 64 |
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 typesThe extended integer types are implementation-defined. Note thatfixed width integer types are typically aliases of the standard integer types. | (since C++11) |
true
orfalse
. The value ofsizeof(bool) is implementation defined and might differ from1.Character types are integer types used for a character representation.
[
0,
255]
, 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.
| (since C++11) |
| (since C++20) |
Besides the minimal bit counts, the C++ Standard guarantees that
≤
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.
The following three types and their cv-qualified versions are collectively called standard floating-point types.
Extended floating-point typesThe extended floating-point types are implementation-defined. They may includefixed width floating-point types. | (since C++23) |
Floating-point types may supportspecial values:
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.
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:
T
through the most positive finite floating-point number representable inT
.T
, the range ofT
is extended to all negative real numbers.T
, 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.
Type | Size in bits | Format | Value range | |
---|---|---|---|---|
Approximate | Exact | |||
character | 8 | signed | −128 to127 | |
unsigned | 0 to255 | |||
16 | UTF-16 | 0 to65535 | ||
32 | UTF-32 | 0 to1114111 (0x10ffff) | ||
integer | 16 | signed | ± 3.27 · 104 | −32768 to32767 |
unsigned | 0 to6.55 · 104 | 0 to65535 | ||
32 | signed | ± 2.14 · 109 | −2,147,483,648 to2,147,483,647 | |
unsigned | 0 to4.29 · 109 | 0 to4,294,967,295 | ||
64 | signed | ± 9.22 · 1018 | −9,223,372,036,854,775,808 to9,223,372,036,854,775,807 | |
unsigned | 0 to1.84 · 1019 | 0 to18,446,744,073,709,551,615 | ||
binary floating- point | 32 | IEEE-754 |
|
|
64 | IEEE-754 |
|
| |
80[note 1] | x86 |
|
| |
128 | IEEE-754 |
|
|
Note: actual (as opposed to guaranteed minimal) limits on the values representable by these types are available inC numeric limits interface andstd::numeric_limits.
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:
64 bit systems:
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).
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_unicode_characters | 200704L | (C++11) | New character types (char16_t andchar32_t) |
__cpp_char8_t | 201811L | (C++20) | char8_t |
202207L | (C++23) | char8_t compatibility and portability fix (allow initialization of(unsigned) char arrays fromUTF-8 string literals) |
void,bool,true,false,char,char8_t,char16_t,char32_t,wchar_t,int,short,long,signed,unsigned,float,double
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 238 | C++98 | the constraints placed on a floating-point implementation was unspecified | specified as no constraint |
CWG 1759 | C++11 | char is not guaranteed to be able to represent UTF-8 code unit 0x80 | guaranteed |
CWG 2689 | C++11 | cv-qualifiedstd::nullptr_t was not a fundemental type | it is |
CWG 2723 | C++98 | the ranges of representable values for floating-point types were not specified | specified |
P2460R2 | C++98 | wchar_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 |
C documentation forarithmetic types |