C++ guides
Modern C++ guides: C++20 and C++23 features, compilers, build systems, and measured performance.
-
What Does std::generator Cost, Measured?
Summing 10 million values costs 0.024 seconds through std::generator and 0.014 through a plain loop on GCC 14 — 1.6 to 1.8 times — while a views::iota pipeline ties the loop at zero allocations, and a second pass over a consumed generator is undefined behavior that segfaults on this libstdc++.
-
How Fast Do std::latch and std::barrier Wake Threads? Measured — 9 to 68 Times Faster Than a Condition Variable
On the same Linux box, std::latch wakes a waiting thread in 364 nanoseconds where a mutex and condition_variable take 4,342 — 11.9 times slower at 2 threads, 68.1 times at 8. std::barrier beats a hand-rolled condition-variable barrier by 1.4 to 1.8 times per phase. The published latency tables assert both numbers without measuring either.
-
GCC 16.2: std::print, ranges::to, and from_range — What Actually Compiles, and the Macro That Does Not Exist
On GCC 16.2.0, std::print, ranges::to, and the from_range constructor all compile and run, and std::hazard_pointer fails at the include because the header is not shipped. The macro that tests from_range is __cpp_lib_containers_ranges, not __cpp_lib_ranges_from_range, which is not a standard macro name at all.
-
C++20 Coroutines + io_uring: A co_await File Read That Actually Runs
A hand-rolled C++20 coroutine reads a file through io_uring with no coroutine library: one pump, 36 bytes, content verified. liburing 2.16 built from source, GCC 13.3.0, and the two fixes the build needed.
-
Ninja vs GNU Make, Measured: the Clean Build Is a Wash — the No-Op Isn't
Same 200-file C++ project, same compiler, 32-way parallelism: the clean build is a wash (Ninja 3.437 s vs Make 3.566 s), the no-op rebuild is 11 times faster under Ninja (0.014 s vs 0.154 s), and both binaries are byte-identical. The measured table, and why the old 20 percent clean-build claim does not reproduce.
-
How Fast Does std::jthread Actually Stop? Measured Shutdown Latency
A std::jthread worker that polls its stop token every 100 ms takes a median 46 to 73 ms to stop after request_stop. The same worker waiting on a condition variable stops in about 0.10 ms. Measured on GCC 13 and Clang 18.
-
Writing Git Packfiles in C++: Deltas, Indexes, and the Streaming Win
A C++ program writes a three-object Git packfile in 221 bytes, hand-encodes REF_DELTA deltas that real Git accepts, and streams a 5 MB blob at 2.94x lower peak memory than the in-memory Python version.
-
Write Git Objects by Hand: A Mini-git in C++
A C++ program writes loose git objects by hand, and git agrees with every hash: blob, tree, and commit checked with hash-object, mktree, cat-file, and fsck.
-
std::formatter That Compiles: A Clang-Only Specialization Project
A working std::formatter specialization project under Clang 18: a quoted-string formatter, a delegating formatter for a point type, the compile-time versus runtime error split, and the range-formatting verdicts that only a GCC 16 frontend can give, because GCC 16 with C++23 does not assemble.
-
Three std::mdspan Traps: The Missing operator(), submdspan in C++26, and assert
Three std::mdspan traps, each proved by a compiled program: libstdc++ 16 ships no operator(), submdspan runs only in C++26, and assert() breaks on a comma in a subscript.
-
GCC 16 Defaults to C++20: What Breaks in Your Old Code
GCC 16.2.0 compiles C++ as C++20 unless told otherwise. A five-idiom legacy codebase fails with 6 error lines under the new default and runs clean under -std=c++17 and GCC 13. This guide runs each breakage, quotes the errors verbatim, corrects three supposed breakages that are not, and shows the fixed code compiling on both.
-
C++20 Coroutine Pitfalls: Three Fixtures, Three Compilers, Same Answer
Three small coroutine programs, each run on GCC 13, GCC 16, and Clang 18 at C++20 with byte-identical output: eager versus lazy start, frame destruction on an early exit, and a lazy task that runs nothing until resumed.
-
Testing C++20 Modules: Direct Commands, Then CMake and CTest
Test a C++20 module with direct compiler commands first, then CMake and CTest: the GCC and Clang commands that work, the GCC 13 configure failure quoted verbatim, and the GCC 14 or newer floor for module scanning.
-
Installing a Header-Only C++20 Modules Library with CMake
CMake rejects INTERFACE libraries with a CXX_MODULES file set. This guide shows the failure on CMake 3.28.3, then installs the same module as a static library and consumes it with find_package — every step run.
-
CMakePresets and vcpkg on a Real C++20 Modules Project
Four preset and vcpkg decisions on a real C++20 modules project: where VCPKG_ROOT lives, manifest versus classic mode, chainloading a second toolchain, and preset inheritance with GCC 16.
-
The C++20 Spaceship Operator: One Defaulted <=> Replaces Six Comparisons
Migrate a small value type from six hand-written comparison operators to one defaulted C++20 three-way comparison — with the signed/unsigned, NaN, and missing-
traps. -
Build a C++ Ranges Pipeline: C++20 Views, Then C++23, With the Compiler Matrix
Build one log-record pipeline twice: C++20 filter, transform, drop, and take over a named owner container, then the C++23 upgrade with enumerate, zip, chunk_by, slide, stride, and cartesian_product, ending in ranges::to — tested on GCC 13, 14, 16, and Clang 18.
-
Build Your Own Mini-Git: Objects, Trees, Commits, Packfiles
Reimplement Git's storage model in stdlib Python so your program produces hashes and objects real Git accepts — loose objects, trees, commits, tags, and a hand-written packfile, all verified against Git 2.43.0.
-
Migrating a Template Library from SFINAE to C++20 Concepts
Migrate a small describe() library from enable_if to C++20 concepts in three steps, keeping the runtime output identical — with the real compiler errors before and after, and the subsumption trap that decides whether the migration works.
-
GCC 16 Defaults to C++20
GCC 16 changes the default C++ standard to C++20. Pin first, then fix each breakage class in order. Every error below was produced on GCC 16.2.0.
-
C++23 std::mdspan and Deducing This
Build a matrix and image view library with C++23 std::mdspan: row-major, column-major, and strided channel views, plus deducing this member functions. Tested on GCC 16, 14, and 13, with the failures quoted.
-
C++23 std::expected Error-Handling Pipelines
Build a CSV parser whose every fallible step returns std::expected and chains with and_then, transform, or_else, and transform_error — with the same-error-type rule, the void step, and the real compiler results on GCC 13 and Clang 18.
-
C++20 Modules with CMake 3.28 and Ninja
Build a small C++20 geometry library as a named module with partitions, wired through CMake 3.28 and Ninja, and tested on Clang 18 and GCC 13 — including the setups that fail.
-
C++20 Coroutines to C++23 std::generator
Write a coroutine generator from scratch with a promise type, Task, and Generator, then replace it with C++23 std::generator and pipe it through ranges views — tested on GCC 13 and GCC 14, with the GCC 13 failure and the single-pass crash shown.
-
The binding that copied
A short C++ program with a bug: the binding that copied. The answer, the observed output, the fix, and the lesson are in this tutorial.
-
The overload that hid the other one
A short C++ program with a bug: the overload that hid the other one. The answer, the observed output, the fix, and the lesson are in this tutorial.
-
auto that did not copy
A short C++ program with a bug: auto that did not copy. The answer, the observed output, the fix, and the lesson are in this tutorial.
-
The future nobody kept
A short C++ program with a bug: the future nobody kept. The answer, the observed output, the fix, and the lesson are in this tutorial.
-
The derived object that arrived as a base
A short C++ program with a bug: the derived object that arrived as a base. The answer, the observed output, the fix, and the lesson are in this tutorial.
-
A declaration disguised as a variable
A short C++ program with a bug: a declaration disguised as a variable. The answer, the observed output, the fix, and the lesson are in this tutorial.