I followed my own advice from “How to get back into C++” and read two books from the list, Bjarne Stroustrup’s “A Tour of C++” and Jason Turner’s “C++ Best Practices.” Both are short (256 and 130 pages) and concrete. I managed to read both in a day.
I list both of these books together since they complement each other well: one shows what is available, and the other shows how to best use it.
“A Tour of C++” (Second Edition)
by Bjarne Stroustrup
When I first started learning C++, I chose “Programming: Principles and Practice Using C++” over the Tour because I needed a tutorial more than a reference. Now, having read the Tour, I can say it was the correct choice.
This book reads much more like an encyclopedia than a textbook, and doesn’t cover some of the more basic C++ features at all, the reader is expected to be already familiar with the language in general. Instead, it puts more recent and advanced features from the later standards (C++14 and C++17) into focus—but that’s why we’re here, aren’t we?
Considering style and tone, you either like Stroustrup’s writing or you don’t. I happen to like it and his attempts at joking, particularly the chapter epigraphs (the very first one is a quote from Henry VI, “The first thing we do, let’s kill all the language lawyers.”) :) He’s optimistic about the state and progress of C++ over the years and it is surprisingly contagious.
There were some points that I found particularly interesting, because I never went too deep into the newer standards:
- I didn’t know you could use a single quote (
'
) as a digit separator. - I forgot the switch statement didn’t require a default clause (I thought it did).
- The book does a good job explaining the purpose of various methods involved with the Rule of 5 (copy and move assignment and construction, etc.). I was struggling with them previously.
- There is a short explanation of how C++20 modules work, which I wouldn’t have read otherwise.
- Finally, an explanation of templates that helped me remember the syntax. Also, template deduction guides—I didn’t know about those.
- Learned about
if constexpr
that I didn’t know about previously. - The concepts overview was (conceptually) interesting but I can’t use C++20 in my current project. Still, I wouldn’t have read about them otherwise and this short intro was clear.
- Realized that I still don’t properly understand variadic templates, even after another explanation. The new fold expressions don’t make them any simpler.
- At the same time, I immediately see how argument forwarding is useful because I have struggled with the same problem in another language. Glad to see it taken care of in C++.
- Finally got a good explanation of the difference between
map
andunordered_map
. - The book has an overview of how iterators are implemented and how the reader might implement their own to make their classes available to the Standard Library algorithms.
- Learned about
mem_fn
(calling a member function as a regular function), didn’t know about it before. Not like it’s a new function, but it’s new to me. - The chapter on numerics wasn’t particularly useful, but it showed how to properly use a random number generator (I previously used the C version).
- Chapter 16 covers the history of C++ and has a neat short list of major features introduced in each of the versions of the standard, and can serve as a quick refresher.
Overall, I quite liked the book and it was exactly what I was looking for after a year’s break of not writing or reading any C++. It is a short, condensed work that covers the language and the Standard Library in sufficient detail to do work, but isn’t as short as a cheatsheet (cheatsheets assume you already know what you are looking for).
“C++ Best Practices”
by Jason Turner
I’m sure this is the most practical and immediately useful C++ book you will ever read. There are 46 (45 and a bonus one) rules, almost each one with an exercise (or several exercises) that urge you to go and apply the rule to your own projects. These rules are not meant to be read once, but to be practised, which is why the book is called “C++ Best Practices” (I just made this up but it sounds plausible).
Since C++ makes it easy to shoot both of your feet off, the author encourages readers to make their own small experiments, poke at them in Compiler Explorer and store them for later. You know the brainteasers that are supposed to gauge how well you know the language? This practice of answering your own questions is much better because it puts you in the mode of discovery, rather than ignoring how exactly something works and hoping for the best. Every programmer is supposed to do it, but do we? This is a gentle reminder that we should, and how to do it in a low-key and useful way.
I like that one of the first rules is “Slow Down.” You can use it for any programming, not just C++. The key points are: don’t pick the first solution that comes to mind, and implement what you’re happy with. Spending extra time thinking up front is usually the difference between a solution that “works,” and a truly elegant solution. Of course, there is the other end of the spectrum (architecture astronautics), but if you read this book, you probably care about code efficiency and aren’t at risk of going that way.
The book has a lot of links to other resources that explain and expound on the rules, so if you don’t understand something or want to learn more, there is additional information to look at.
If you’re fairly new to C++ (like me), some rules may be surpising: “You can do that?” I mean that question in a good way—it turns out, you can do something you thought you couldn’t.
I have implemented some of the advice before (adding a full set of warnings, clang-format, clang-tidy) and it certainly has made my code much better—and safer. I’m looking forward to using these rules and coming back to the book often. It’s just 130 pages so you can quickly flip through it to jog your memory.