Learn C++ by Example • Frances Buontempo & Matt Godbolt
Frances Buontempo discusses her book "Learn C++ by Example," explaining her teaching philosophy which uses self-contained, playable examples to make modern C++ features like coroutines, the spaceship operator, and structured bindings accessible to programmers returning to the language.
Frances Buontempo’s book, “Learn C++ by Example,” is primarily aimed at programmers who may have known C++ in the past but have lost track of the language’s evolution since the C++11 standard. The goal is to provide a concise, 225-page guide to “relearn” C++ by focusing on common use cases and features that simplify modern code.
A Practical Approach to Teaching
Buontempo’s teaching philosophy, influenced by her father who was a teacher educator, centers on using self-contained, playable examples. The book contains small projects, like a high-low card game or a slot machine, that allow readers to hold the entire concept in their heads and experiment freely. This approach is born from the idea that learners engage more deeply when they can “play with the ideas,” such as changing the number of lives in a game—a method Buontempo herself used to learn programming.
This contrasts with typical reference books that often use abstract examples with “foos or widgets.” Instead, the focus is on providing practical, memorable scenarios that give context to complex features. This is exemplified by her well-known series of talks, “X Out of a Y Paper Bag,” which originated from a colleague’s offhand comment. The simple, visualizable problem of coding an escape from a paper bag provides a perfect sandbox for demonstrating concepts in object-oriented programming and even machine learning.
Demystifying Modern C++ Features
The book tackles several modern C++ features designed to reduce boilerplate and simplify code, though they can be hard to look up due to their syntax.
- The Spaceship Operator (
<=>): This is highlighted as a key piece of “syntactic sugar.” It automatically generates all six comparison operators for a type, which is particularly useful for using custom types in containers likestd::map. The book points to tools like Compiler Explorer and CppInsights.io to help developers understand the code the compiler generates behind the scenes. - Structured Bindings: This feature is introduced as a way to write less code by unpacking elements from pairs or tuples directly into named variables, such as the key and value from a map lookup.
- Coroutines: Acknowledged as one of the more complex modern features, coroutines are introduced through a practical lens. The book covers their use in creating generators, similar to Python’s
yield, which can simplify code for state machines. By yielding results at different points, a coroutine can represent a state machine without the convolutedif/elseorswitchblocks, making the code easier to reason about. While writing a coroutine from scratch can be complex (“like a kit car”), using standard library features likestd::generatorprovides an accessible entry point. - “Almost Always Auto” Style: The book presents the
autokeyword as a common and useful tool for type deduction, but maintains a balanced perspective. It acknowledges the potential pitfalls, such asautodeducing an initializer list or causing unintended copies. Therefore, whileautois used, types are sometimes stated explicitly for clarity.
The Evolution and Future of C++
The discussion touches on the continuous evolution of C++, with new standards released every three years. A significant upcoming feature for C++26 is static, compile-time reflection. This will allow code to inspect properties of types. This capability is foundational for proposals like Herb Sutter’s metaclasses, which aim to use reflection to generate boilerplate code automatically. For instance, a developer could annotate a class as non-copyable, and the compiler would generate the necessary special member functions, reducing errors and making the language more declarative.
Even as an experienced author, Buontempo finds that explaining C++ can be challenging. She is currently writing a new book aimed at complete beginners and notes the difficulty of explaining seemingly paradoxical concepts like a “constant variable,” which underscores the language’s inherent complexity.