C++ primitive types like
char
,short
, orint
are the heritage of primitive types in the C programming language. C++ adds a short list of its own primitive types likebool
plus a long list of non-primitive types likestd::string
; it is the possibility of introducing new types based on, mostly, theclass
construct/keyword.
Rule(s)
- In C++, primitive types are (
bool
withfalse
andtrue
as possible values),char
,wchar_t
,char16_t
,char32_t
,short
(at least 16-bit long),int
(and variants),long
(at least 32-bit long),long long
(at least 64-bit long),float
,double
andlong double
.signed
(default) andunsigned
may be used as modifiers forchar
,short
,int
,long
and,long long
. Theunsigned
modifier ignores sign bit.void
is a special type in the sense thatvoid variable;
is forbidden whilevoid* pointer;
is allowed.- Other common (non-primitive) type is the string type:
char*
andstd::string
(with#include <string>
).- Depending upon computer architecture and Operating System (OS), the size of a C++ type or variable (in bytes) may be computed with the help of the
sizeof
operator. Type variation intervals are available through predefined constants. For example, [SHRT_MIN
SHRT_MAX
] is the variation interval ofshort
.Example
Rule(s)
char
variants require specific management with value prefixes that characterize types (note that the same applies forstd::string
variants likestd::wstring
).Example
Example
See also
Numbers representation
Rule(s)
- Most programming languages require a way of representing primitive types (numbers especially) in memory. Compilers crucially rely on type representation to instrument calculations: +, -, *, /, …
- C++ does not impose a unique representation for numbers (integers and floating point numbers) even though the IEEE 754 standard is often used for floating point numbers.
Example (short integers) Numbers.Cpp.zip
Example (floating point numbers) Numbers.Cpp.zip
Numerical stability
Rule(s)
- From C++11, the numerical stability of calculations may be controlled on a fine-grain basis. On purpose, constants and functions are provided.
Example Numbers.Cpp.zip
Rule(s)
- C++11 comes with a lot of non-primitive types. A key enhancement is embodied by the
<chrono>
header file that contains time-related types beyond the old-fashion types in the historical<ctime>
header file.Predefined types: the case of
<chrono>
Example (old school)
Rule(s)
<chrono>
key types arestd::chrono::duration
(with instantiations:std::chrono::nanoseconds
,std::chrono::microseconds
…),std::chrono::time_point
,std::chrono::system_clock
… with direct interoperability withstd::time_t
andstd::tm
in<ctime>
.Example
Example (elapsed time measurement)
Predefined types: the case of
std::filesystem::path
(C++17)Rule(s)
<filesystem>
comes with simplified file manipulation (get file properties as prefix, suffix, size…), which hides the underlying Operating System -OS-.Example (working directory of running process)
Example
See also
- C++17 file system API here…
Data structures
Rule(s)
- User-defined types like data structures are constructed by means of the
struct
,union
orclass
keywords. Onlyclass
should be used sincestruct
andunion
are old-fashion C style: properties insidestruct
-based andunion
-based data structures are nothing else thanpublic:
.Example (
.h
header file) Numbers.Cpp.zipExample (
.cpp
header file) Numbers.Cpp.zipAggregate initialization
Rule(s)
- From C++11, curly braces may instrument object initialization in a straightforward way. Curly brace-based initialization relies on constructors in an implicit way or on
std::initializer_list
in an explicit way.Example (implicit) Numbers.Cpp.zip
Example (explicit) Lambda_expression_.Cpp.zip
User-defined suffixes
Rule(s)
- From C++11, literals (integers, characters, floating-point numbers, strings, booleans and pointers) may be suffixed to reinforce the type of a literal towards an existing (see
<chrono>
above) or user-defined type.Example (existing versus user-defined literal suffix) Inline_namespace.Cpp.zip
See also
Rule(s)
- C++ arrays are managed as common pointers. However, C++ increases type checking at compilation time that prevents potential errors at run-time.
Example
Rule(s)
- Beyond initialization time, C++ arrays may grow (or shrink) on demand.
Example
Rule(s)
- Enumerated types raise the problem of matching to
int
. Recent improvements occur through scoped enumerated types.Example (non-scoped)
Example (scoped)
Example (
char
as backbone type: integral types only!)See also
- More on C++ enumerated types in this blog
auto
(from C++ 11),
register
(until C++ 17, but already obsolete), extern
, static
,
mutable
, and thread_local
See also
Rule(s)
- Programming languages with strong (static) type checking like C++ require type declaration. Type inference is the ability to deduce the type of a variable, reducing verbose type declaration.
auto
keywordRule(s)
auto
is an old C construct. It is the “automatic” storage mechanism. Since it is the default storage mechanism,auto
was given up in C++11 to get another usage, i.e., type inference.- From C++11, variables no longer need type declaration when
auto
is used.Example
decltype
keywordRule(s)
decltype
is a compilation-based operator that deduces the type of an expression.Example N_INSEE.Cpp.zip
Example Type_inference.Cpp.zip
Rule(s)
decltype(e)
deduces a type frome
as follows:E
whene
is a “prvalue”,E&
whene
is a “lvalue” and,E&&
whene
is a “xvalue”.Example
Rule(s)
std::declval
comes as an utility fordecltype
. It is essentially useful whendecltype
is concerned with generic types in templates.Example Type_inference.Cpp.zip
Example Smart_pointers.Cpp.zip
See also
decltype
andstd::declval
(in French)Structured binding
Rule(s)
- Like a reference, a structured binding is an alias to an existing object. Unlike a reference, the type of a structured binding does not have to be a reference type.
Example Lambda_expression_gcc.Cpp.zip
Type casting Rule(s)
- Type casting (a.k.a. type conversion) is a key issue in daily programming. The robustness of the C++ typing system depends on casting variables that really conform to each other based on, beyond their compilation-time type, their runtime type.
- Controlled type casting relies on operators, and possibly further implementation of type-to-type interoperability.
Type interoperability
Rule(s)
explicit
keyword prevents implicit conversion and thus undesired interoperability between types.Example
Aware casting
Rule(s)
- C++ supports aware casting, which relies on the
static_cast
,reinterpret_cast
(no calculation compared tostatic_cast
),dynamic_cast
and,const_cast
operators. They all convert pointers or references.Example (
static_cast
)Example (
const_cast
)Example (
dynamic_cast
)Towards safer conversions
Rule(s)
- C++11 comes with the possibility of user-defined explicit conversions including template-based conversions.
Example Advanced_genericity.Cpp.zip
Introspection based on Run Time Type Information -RTTI-
Rule(s)
- C++ offers a lightweight infrastructure for introspection, which is called RunTime Type Information or RTTI (
#include <typeinfo>
).- C++ compilers like g++ generally allows RTTI by default. Nonetheless the
-fno-rtti
option of g++ disables RTTI, i.e.,typeid
anddynamic_cast
.Example RTTI.Cpp.zip