Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Home/C++/Tips & tricks
C++

C++

Zero-cost abstractions and full control over memory.

Paradigm
Multi-paradigm, OOP, generic
Typing
Static, strong-ish
Memory
Manual / RAII, no GC
Runs
Native machine code (AOT)
Concurrency
Threads, atomics, coroutines (C++20)

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 cost

Saved in this browser only. Part of Clean code foundations (0/5).