Values, initialization, and control flow
Representations, narrowing, overflow, branch invariants
A C++ object has a type, storage, a lifetime, and a current value. int count; at block scope leaves count uninitialized; reading it is not a random-number API. Prefer int count{}; when zero is a meaningful initial state, or initialize directly with the value establishing the invariant. Braces reject narrowing that parentheses and implicit conversion may accept.
int count{}; // initialized zero
long long bytes{4096};
auto ratio = 5 / 2; // int, value 2
// int truncated{3.5}; // ill-formed narrowingauto deduces a type; it does not select a type based on the value's future use. Integer division happens before assignment to a floating variable. Signed arithmetic overflow is undefined behavior, while unsigned arithmetic wraps modulo its range. Mixing a negative integer with size_t can convert the negative value to a huge positive number. Validate signed data before treating it as an index.
Control flow should establish a fact used by the next statement. if(optional) checks presence, so an optional containing zero is true. A loop that consumes digits should preserve the invariant that its accumulator represents exactly the consumed prefix. Check bounds before multiply/add, rather than computing an overflowing result and checking afterward.
std::optional<Int> maybe_zero{0};
if (maybe_zero) { /* value exists, even though *maybe_zero == 0 */ }Decimal parsing also exposes the difference between lexical policy and numeric range. Decide whether signs, whitespace, empty strings, and leading zeroes are accepted. Library parsing is useful in application code, but implementing a small strict parser makes each control-flow decision visible. In the final exercise the negative minimum has one greater magnitude than the positive maximum; accumulating only into a positive signed Int rejects that legitimate endpoint. An unsigned magnitude or negative accumulator can model it safely.
Put it into practice
Implement the exercise in a full-screen workspace, then run its tests.