S R T B H P N

C++ Type System

Some of this content was summarized from cppreference.com and Wikipedia


Fundamental Types


Incomplete Types
  • can't create instances, arrays, or references.
  • can create pointers to, and return from, functions.
examples: void, class X;
Void type Incomplete type with an empty set of values.
std::nullptr_t Type of the null pointer literal, nullptr. It is not a pointer type.
Boolean type Holds one of two values: true, false
Integer types integer types: short int, int, long int, long long int qualifiers: signed, unsigned, const, static, volatile
Character types character types: char, wchar_t, char16_t, char32_t qualifiers: signed, unsigned, const, static, volatile
Floating point types float types: float, double, long double qualifiers: const, static, volatile
Type keywords void, bool, true, false, char, whcar_t, char8_t, char16_t, char32_t, int, short, long, signed, unsigned, float, double
Type qualifiers const, static, volatile, mutable
C++14 - Wikipedia introduced or modified a number of type related constructs, e.g., auto, decltype, constexpr, binary literals, digit separators, standard user-defined literals, and tuple addressing by type. C++17 - Wikipedia made several smaller additions and modifications.

Type Systems


Type Sizes, Win64, C++17
Strong Typing Strongly typed languages have relatively strict rules at compile time, favoring errors and exceptions at compile-time.
Dynamic Typing Dynamically typed languages may have relatively strict rules, but they are evaluated at run-time. The good news is that construction is flexible and quick. The bad news is that debugging is more frequent, and frequently irritating.
Duck Typing "In strongly type languages, suitability is based on an object's type. In weakly typed languages suitability is determined by the presence of needed methods and properties." "If it walks like a duck and quacks like a duck, it probably is a duck" wikipedia
Casts, Promotions, and Coercions "Many programming languages support the conversion of a value into another of a different data type. [These kinds] of type conversions can be implicitly or explicitly made." "Implicit conversion, which is also called coercion, is automatically done. Explicit conversion, which is also called casting, is performed by code instructions." wiki books, cppreference.com CppBasicDemos has the code - CastTests - that generated output shown in the figure, below. C++ casts:
  • static_cast: - creates a new object of type T2 from one of type T1 T1 t1;
    T2 t2 = static_cast<T2>(t1);
  • const_cast: - strips const qualifier const T t1;
    T t2 = const_cast<T>(t1);
  • dynamic_cast: - recovers derived type from base pointer or reference D d;
    B* pD = &d;
    D* pD1 = dynamic_cast<D*>(pD);
    if(pD1)
      // can use pD1 to call D specific methods
  • reinterpret_cast: - interprets object as having different type using byte = char;
    double d = 3.1415927;
    byte* pBlock = reinterpret_cast<byte*>(&d);
      // pointer to first byte in d