Lesson 6 of 9 · 28m
The Maintainable Subset of C++17/20/23
Choose language features that reduce local reasoning cost — concepts, spans, structured bindings, ranges where readable — and reject cleverness that hides invariants.
C++ is enormous. The mistake is assuming that because a feature exists, your codebase should use it. Senior practitioners do not write 'modern' C++ by maximizing feature count; they choose a subset that makes ownership, constraints, and invariants obvious to the next engineer. That engineer may be you at 3 a.m. during a rollback.
Use concepts to name requirements
Templates once failed with thousand-line substitution errors. C++20 concepts turn requirements into interface documentation:
template<typename T>
concept ByteSink = requires(T sink, std::span<const std::byte> data) {
{ sink.write(data) } -> std::same_as<bool>;
};
template<ByteSink Sink>
bool send_frame(Sink& sink, std::span<const std::byte> frame) {
return sink.write(frame);
}This is maintainable because the abstraction says what it needs. Do not build a concept taxonomy for its own sake. Name concepts that appear in real APIs and encode actual semantic requirements, not just syntactic accidents.
structured bindings and auto: clarity over austerity
for (const auto& [key, value] : index) {
if (value.expired()) stale.push_back(key);
}Structured bindings are excellent when names make the decomposition meaningful. They are noise when they expose internal layout or produce meaningless names like auto [a, b].
The same applies to auto. Use it when the type is obvious from the right-hand side or irrelevant to local reasoning:
auto now = std::chrono::steady_clock::now();
auto it = sessions.find(id);Do not use it to obscure conversion behavior in arithmetic-heavy code or public interfaces. auto never means 'I do not care what type this is'; it means 'the reader can already tell.'
ranges are a tool, not a style badge
Ranges can make filtering pipelines readable:
auto active = users
| std::views::filter([](const User& u) { return u.enabled; })
| std::views::transform([](const User& u) { return u.id; });They can also hide complexity, iterator category requirements, and lifetime traps behind a cute pipeline. A view does not own the underlying data. Passing a range pipeline to asynchronous code without understanding its lifetime is the modern version of storing a dangling reference.
Prefer whichever form is easier for the team to debug. A plain loop is not legacy code; sometimes it is the correct abstraction.
enum class, optional, variant, and expected
Replace primitive obsession with small typed vocabulary types:
enum class Permission { read, write, admin };
using UserId = strong_type<std::uint64_t, struct user_id_tag>;
using LookupResult = std::variant<User, NotFound, LockedOut>;std::variant is useful when the alternatives are closed and callers must handle them. It is not a replacement for polymorphism when behavior is open-ended. enum class prevents accidental integer conversion. Small wrappers prevent parameter-order bugs, but only adopt a strong-type library when the cost is justified by real misuse.
Know what to avoid
Deep template metaprogramming, expression templates, clever operator overloading, inheritance hierarchies with accidental virtuals, and macro-generated APIs all compress code at the cost of expanding cognitive surface. Sometimes the compression is worth it — serialization frameworks and numeric libraries exist — but application code rarely earns that complexity.
Field notes
- Compile time is part of maintainability; unnecessary header-only template exposure can make small changes expensive.
if constexpris useful inside templates, but it does not eliminate invalid operations in discarded branches for dependent code the way newcomers expect.- Do not use
std::anyfor values whose type is statically knowable; it moves type errors from compile time to runtime. - If a language feature requires a paragraph of local explanation every time it appears, it is probably not in your maintainable subset.
Was this lesson helpful?