Espacios de nombres
Variantes

Tipos fundamentales

De cppreference.com
Revisión del 15:48 2 feb 2020 de Ljestrada (discusión | contribs.) (Traducción de secciones iniciales Tipo void, nullptr_t, Tipo Booleano, Modelos de Datos, Tipos Enteros)

(Véase también type para un resumen del sistema de tipos y la lista de utilidades relacionadas con tipos que se suministran por la biblioteca de C++)

Tipo void

void - tipo con un conjunto de valores vacío. Es un tipo incompleto que no puede completarse (consecuentemente, objetos de tipo void no se permiten). No existen arrays de void, ni referencias a void. Sin embargo, se permiten punteros a void y funciónes que devuelven tipo void (procedimientos en otros lenguajes).

std::nullptr_t

Definido en el archivo de encabezado <cstddef>
typedef decltype(nullptr) nullptr_t;
(desde C++11)

std::nullptr_t es el tipo del literal de puntero nulo, nullptr. Es un tipo distinto que no es en sí mismo un tipo de puntero o un tipo de puntero a miembro.

Tipo Booleano

bool - tipo capaz de manetener uno de dos valores: true o false. El valor de sizeof(bool) está definido por la implementación y puede diferir de 1.

Modelos de datos

Las elecciones hechas por cada implementación sobre los tamaños de los tipos fundamentales, colectivamente conocidos como modelos de datos. Cuatro modelos de datos fueron ampliamente aceptados:

Sistemas de 32 bits:

  • LP32 or 2/4/4 (int es de 16 bits, long y puntero son de 32 bits)
  • Win16 API
  • ILP32 or 4/4/4 (int, long, y puntero son de 32 bits);
  • Win32 API
  • Unix y sistemas similares a Unix (Linux, Mac OS X)

Sistemas de 64 bits:

  • LLP64 or 4/4/8 (int y long son de 32 bits, puntero es de 64 bits)
  • Win64 API
  • LP64 or 4/8/8 (int es de 32 bits, long y puntero son de 64 bits)
  • Unix sistemas similares a Unix (Linux, Mac OS X)

Otros modelos son muy raros. Por ejemplo, ILP64 (8/8/8: int, long, y puntero son de 64 bits) solamente aparecieron en algunos modelos de sistemas iniciales de Unix de 64 bits (p. ej., Unicos en Cray).

Tipos enteros

int - tipo entero básico. La palabra clave int puede omitirse si se usa cualquiera de los modificadores listados abajo. Si no se encuentran presentes modificadores de longitud, se garantiza que tendrá al menos una anchura de 16 bits. Sin embargo, en sistemas de 32/64 bits, casi siempre se garantiza que tendrá una anchura de al menos 32 bits (véase abajo).

Modificadores

Modifica el tipo entero. Puede mezclarse en cualquier orden. Solamente uno de cada grupo puede estar presente en el nombre del tipo.

Signo

signed - El tipo destino tendrá una representación con signo (por defecto si se omite).
unsigned - El tipo destino tendrá una representación sin signo.

Tamaño

short - El tipo destino se optimizará por espacio y tendrá una anchura de al menos 16 bits.
long - El tipo destino tendrá una anchura de al menos 32 bits.
long long - El tipo destino tendrá al menos una anchura de 64 bits.
(desde C++11)

Nota: al igual que todos los especificadores de tipo, se permite cualquier orden: unsigned long long int y long int unsigned long se refieren al mismo tipo.

Propiedades

La siguiente tabla resume todos los tipos enteros disponibles y sus propiedades en varios modelos de datos comunes:

Especificador de tipo Tipo equivalente Anchura en bits por el modelo de datos
C++ standard LP32 ILP32 LLP64 LP64
short
short int al menos
16
16 16 16 16
short int
signed short
signed short int
unsigned short
unsigned short int
unsigned short int
int
int al menos
16
16 32 32 32
signed
signed int
unsigned
unsigned int
unsigned int
long
long int al menos
32
32 32 32 64
long int
signed long
signed long int
unsigned long
unsigned long int
unsigned long int
long long
long long int
(C++11)
al menos
64
64 64 64 64
long long int
signed long long
signed long long int
unsigned long long
unsigned long long int
(C++11)
unsigned long long int

Nota: la aritmética de enteros se define de manera diferente para los tipos enteros con signo o sin signo. Véase operadores aritméticos, en particular desbordamiento de enteros.

std::size_t es el tipo de entero sin signo del resultado del operador sizeof así como del operador sizeof... y el operador alignof (desde C++11).

Véase también Tipos enteros de anchura fija. (desde C++11)

Tipos de carácter

signed char - type for signed character representation.
unsigned char - type for unsigned character representation. Also used to inspect object 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 either signed char or unsigned char, but is always a distinct type). Multibyte characters strings use this type to represent code units. The character types are large enough to represent any UTF-8 eight-bit code unit (desde C++14). The signedness of char 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 (see wide strings). Required to be large enough to represent any supported character code point (32 bits on systems that support Unicode. A notable exception is Windows, where wchar_t is 16 bits and holds UTF-16 code units) It has the same size, signedness, and alignment as one of the integer types, but is a distinct type.
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 as std::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 as std::uint_least32_t, but is a distinct type.
(desde 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 as unsigned char (and. therefore, the same size and alignment as char and signed char), but is a distinct type.
(desde C++20)

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

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

Note: this allows the extreme case in which bytes are sized 64 bits, all types (including char) are 64 bits wide, and sizeof returns 1 for every type.

Tipos de punto (o coma) flotante

Aunque en países como España se utiliza la coma como el separador decimal, esta traducción conserva el uso de punto flotante en lugar de coma flotante.

float - single precision floating point type. Usually IEEE-754 32 bit floating point type
double - double precision floating point type. Usually IEEE-754 64 bit floating point type
long double - extended precision floating point type. Does not necessarily map to types mandated by IEEE-754. Usually 80-bit x87 floating point type on x86 and x86-64 architectures.

Propiedades

Floating-point types may support special values:

  • infinity (positive and negative), see INFINITY
  • the negative zero, -0.0. It compares equal to the positive zero, but is meaningful in some arithmetic operations, e.g. 1.0/0.0 == INFINITY, but 1.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, see std::nan, NAN. Note that C++ takes no special notice of signalling NaNs other than detecting their support by std::numeric_limits::has_signaling_NaN, and treats all NaNs as quiet.

Real floating-point numbers may be used with arithmetic operators + - / * and various mathematical functions from cmath. Both built-in operators and library functions may raise floating-point exceptions and set errno as described in math_errhandling

Floating-point expressions may have greater range and precision than indicated by their types, see FLT_EVAL_METHOD. Floating-point expressions may also be contracted, that is, calculated as if all intermediate values have infinite range and precision, see #pragma STDC FP_CONTRACT.

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

Implicit conversions are defined between real floating types and integer types.

See Limits of floating point types and std::numeric_limits for additional details, limits, and properties of the floating-point types.

Rango de valores

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 -(2N-1
-1)
to +2N-1
-1
(e.g. -127 to 127 for a signed 8-bit type), which corresponds to the limits of one's complement or sign-and-magnitude.

However, all C++ compilers use two's complement representation, and as of C++20, it is the only representation allowed by the standard, with the guaranteed range from -2N-1
to +2N-1
-1
(e.g. -128 to 127 for a signed 8-bit type).

Type Size in bits Format Value range
Approximate Exact
character 8 signed -128 to 127
unsigned 0 to 255
16 unsigned 0 to 65535
32 unsigned 0 to 1114111 (0x10ffff)
integer 16 signed ± 3.27 · 104 -32768 to 32767
unsigned 0 to 6.55 · 104 0 to 65535
32 signed ± 2.14 · 109 -2,147,483,648 to 2,147,483,647
unsigned 0 to 4.29 · 109 0 to 4,294,967,295
64 signed ± 9.22 · 1018 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned 0 to 1.84 · 1019 0 to 18,446,744,073,709,551,615
floating
point
32 IEEE-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
64 IEEE-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


Note: actual (as opposed to guaranteed minimal) limits on the values representable by these types are available in <climits>, <cfloat> and std::numeric_limits

Palabras clave

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

Véase también

Documentación de C para tipos aritméticos