determines the relative ordering of 2 objects: (a <=> b) < 0if a < b (a <=> b) > 0if a > b (a <=> b) == 0if a and b are equal/equivalent 3-way comparisons return a comparison category value that can be compared to literal 0. The returned value comes from one of three possible categories: std::strong_ordering, std::weak_ordering or std::partial_ordering.
// storage bits: sign + exponent + mantissa std::float16_t a = 12.3f16; // 1 + 5 + 10 = 16 bits = 2 B std::float32_t b = 12.3f32; // 1 + 8 + 23 = 32 bits = 4 B std::float64_t c = 12.3f64; // 1 + 11 + 52 = 64 bits = 8 B std::float128_t d = 12.3f128; // 1 + 15 + 112 = 128 bits = 16 B std::bfloat16_t e = 12.3b16; // 1 + 8 + 7 = 16 bits = 2 B
Type Narrowing
conversion from type that can represent more values to one that can represent less
may result in loss of information
in general no compiler warning – happens silently
potential source of subtle runtime bugs
double d = 1.23456; float f = 2.53f; unsigned u = 120u; double e = f; // OK float → double int i = 2.5; // NARROWING double → int int j = u; // NARROWING unsigned int → int int k = f; // NARROWING float → int
Braced Initialization C++11
type variable { value };
works for all fundamental types
narrowing conversion ⇒ compiler warning
double d {1.23456}; // OK float f {2.53f}; // OK unsigned u {120u}; // OK double e {f}; // OK float → double int i {2.5}; // COMPILER WARNING: double → int int j {u}; // COMPILER WARNING: unsigned int → int int k {f}; // COMPILER WARNING: float → int