type
From Cppwiki
A type determines the properties of an object of that type, including its size in bytes, the range of possible values it can take, the operations that it supports, and so on.
Types can also be used as parameters for generic function and class templates, allowing a single template to operate on a wide variety of types. This ability has led to the much broader field known as template metaprogramming.
The sizeof operator can be used to calculate the size, in bytes, of any object or type. sizeof(char) is 1; the sizes of all other types are implementation-defined, subject to some constraints as noted below.
The headers <climits> (<limits.h> in C) and <cfloat> (<float.h> in C) define a battery of macros that specify properties for built-in types. The header <limits> enhances this by defining the traits class template std::numeric_limits, which can easily be extended to new types by specializing for those types.
Also, C++ provides a rudimentary reflection system called runtime type information (RTTI) which can be accessed using the typeid operator (in conjunction with the std::type_info class in <typeinfo>) or the dynamic_cast operator.
Contents |
Fundamental types
Integral types
Additionally, C++ specifies four basic integral types, in order of increasing size:
-
char -
short(orshort int) -
int -
long(orlong int)
char must be at least 8 bits, short and int must be at least 16 bits each, and long must be at least 32 bits. [1]
Each of these types allows the modifiers signed or unsigned. Signed types typically have half the maximum absolute value of unsigned types, but have the ability to represent negative numbers. For instance, a signed 8-bit quantity might have a range from -128 to 127, inclusive, whereas an unsigned 8-bit quantity might have a range from 0 to 255.
Note: Signed types have peculiar semantics when used as operands in bitwise operations. For this reason, it is recommended that you use unsigned types when dealing with the underlying bits rather than the quantities they represent.
For all types but char, the signed keyword is implicit. For instance, int is the same as signed int. On the other hand, char behaves either like signed char or unsigned char, depending on the implementation. However, even on an implementation where (for instance) char happens to behave like signed char, char and signed char are still two distinct types.
Additionally, C++ specifies two more integral types:
-
boolcan represent the valuestrueorfalse. When converting tobool, zero becomesfalse, and any other value becomestrue. -
wchar_tis implemented internally in terms of the basic four types, depending on the implementation. It is also distinct from that type. For instance, even on an implementation where (for instance)wchar_thappens to be implemented in terms ofunsigned short,wchar_tandunsigned shortwould be two distinct types.
Note: Microsoft Visual C++ 6.0 erroneously considers wchar_t as a typedef for unsigned short. This is not compliant with ISO C++.
Floating-point types
C++ specified three floating-point types, listed here in order of increasing precision:
-
float -
double -
long double
The bitwise representation of these types is implementation-defined. Variations on IEEE-754 are typical, but not required.
void
The void type is an incomplete type. Its two primary uses are as the return type of a function that does not return a value, and as part of pointer-to-void, which can be used to point to any type.
Note: There are other uses, such as casting an expression to void to make it clear that the result is not used; or filling an empty function parameter list to make it clear that the function does not accept parameters. However, these are both fairly clear in C++ without the use of void.
Compound types
Arbitrarily complex types can be composed using just the fundamental types and the following:
- arrays contain one or more elements, all of the same type.
- functions use a list of types describing the accepted parameters, as well as the returned result.
- pointers and references can refer to other objects, and also to functions (which are not objects).
- classes and structs can contain member functions and variables and define how they can be accessed.
- unions are similar to classes, but their member variables share the same region of storage.
- enumerations specify a range of values that may be assigned to a variable.
- pointers-to-member can refer to a member function or variable of an object.