Tips & tricks
Small idioms with a big payoff.
Structured bindings
Unpack pairs, tuples and structs directly in a loop or declaration.
std::map<std::string, int> stock{{"apples", 3}, {"pears", 5}};
for (const auto& [name, qty] : stock) { // structured bindings (C++17)
std::cout << name << ": " << qty << '\n';
}std::optional and if-with-initializer
Return “maybe a value” without magic numbers or out-parameters.
#include <optional>
std::optional<User> findUser(int id) {
if (auto it = users.find(id); it != users.end()) // if-with-initializer
return it->second;
return std::nullopt; // no sentinel values
}constexpr and static_assert
Move computation and checks to compile time.
constexpr int square(int x) { return x * x; }
static_assert(square(4) == 16); // evaluated by the compiler, zero runtime costSaved in this browser only. Part of Clean code foundations (0/5).