C++11 has been around for around 11 years and C++14 for 8. From my experience, I see that even today, many companies struggle to use those standards in production in the most efficient way. As always, with new stuff came benefits, risks, and increased learning effort. Fortunately, with a new book written by top C++ Experts, we have a solid guide on what is safe and what might be problematic in Modern C++.

The book is called “Embracing Modern C++ Safely”.

Let’s see what’s inside.

Disclaimer: I got a free copy from the publisher.

The book  

Here are the main links and info about the book:

Embracing Modern C++ Safely

The book on Amazon:

Embracing Modern C++ Safely 1st Edition @Amazon (released December 23, 2021)).

The book page at InformIT:

Embracing Modern C++ Safely | InformIT

Here’s the sample from InformIT (141 pages, PDF):

Download the sample pages - PDF

An article  

You can also read this article from Bloomberg Tech about the project: Why 4 Bloomberg engineers wrote another C++ book | Bloomberg LP

Presentation  

One of the authors, Vittorio, hold a valuable presentation at the recent CppCon 2021. He shared some insights from the book and some stats and described several C++ features and their “safety” profiles.

C++11/14 at Scale: What Have We Learned? - Vittorio Romeo - CppCon 2021

Authors  

The book was written by John Lakos and Vittorio Romeo, with assistance from Rostislav Khlebnikov and Alisdair Meredith.

John Lakos (his profile at InformIT), manager at Bloomberg, runs their BDE group, which develops fine-grained reusable C++ software using his component-based methodology and process. He also mentors engineers and team leads throughout Bloomberg’s software infrastructure department. As a voting member of the C++ Standards Committee, he has helped shape new generations of C++.

Vittorio Romeo (his profile at InformIT) is a senior software engineer at Bloomberg LP who builds mission-critical C++ middleware, trains colleagues on modern C++, and has created many open-source C++ libraries and games.

Dr. Rostislav Khlebnikov (his profile at InformIT) is a team lead and senior software engineer in Bloomberg’s BDE team, where he directs the development of high-performance C++ foundation software.

Alisdair Meredith (his profile at InformIT) is a senior software engineer in Bloomberg’s BDE team, a long-standing member of the C++ Standards Committee, and a former Chair of its Library Working Group between 2010 and 2015.

Let’s see what’s inside the book.

The Structure  

Here’s the simplified TOC:

Chapter 0. Introduction

Chapter 1. Safe Features

  • Attribute Syntax
  • Consecutive >s
  • decltype
  • Defaulted Functions
  • Delegating Ctors
  • Deleted Functions
  • explicit Operators
  • Function static ‘11
  • Local Types ‘11
  • long long
  • noreturn
  • nullptr
  • override
  • Raw String Literals
  • static_assert
  • Trailing Return
  • Unicode Literals
  • using Aliases
  • Aggregate Init ‘14
  • Binary Literals
  • deprecated
  • Digit Separators
  • Variable Templates

In most cases, those features are worth everyone’s time, and introducing them in the code might be relatively easy. What’s more, those features won’t break your code, so it’s hard to write something terrible here.

For example: override - is a no-brainer. Yet, even with those safe things, the book describes all features very well and shows how to use them and their typical use cases.

Chapter 2. Conditionally Safe Features

  • alignas
  • alignof
  • auto Variables
  • Braced Init
  • constexpr Functions
  • constexpr Variables
  • Default Member Init
  • enum class
  • extern template
  • Forwarding References
  • Generalized PODs ‘11
  • Inheriting Ctors
  • initializer_list
  • Lambdas
  • noexcept Operator
  • Opaque enums
  • Range for
  • Rvalue References
  • Underlying Type ‘11
  • User-Defined Literals
  • Variadic Templates
  • constexpr Functions ‘14
  • Generic Lambdas
  • Lambda Captures

This is the longest part, and there are more caveats here. For example, the chapter on Generalized PODs ‘11 is more than 130 pages! The main point of being “conditionally safe” means that those parts will improve your code in most cases, but you have to pay attention to some corner cases.

Chapter 3. Unsafe Features

  • carries_dependency
  • final
  • friend ‘11
  • inline namespace
  • noexcept Specifier
  • Ref-Qualifiers
  • union ‘11
  • auto Return
  • decltype(auto)

Fortunately, the set of features in this chapter is shorter. Those parts have their uses in projects but should be used rarely and very carefully.

Quoting the book:

Features that are classified as unsafe are those that might have valid — and even important — use cases, yet our experience indicates that routine or widespread use would be counterproductive in a typical, large-scale, software-development enterprise.

My View  

I’m still reading this book, and I haven’t read all the pages (1300+ pages!). Yet, I’ve seen all of chapters, read half of them and picked those related to my recent work, tasks, or blog posts. I’m impressed with the level of detail and the quality the authors put into this material. The book has become my primary “reference” for those C++11/14 parts.

In short, this book is a gold mine of great discussions and detailed explanations about Modern C++.

It’s built on the following principles:

  • Facts, Not Opinions
  • Elucidation, Not Prescription - describe various uses cases so that you can choose your path
  • Thorough, Not Superficial
  • Real-World, Not Contrived, Examples
  • At Scale, Not Overly Simplistic, Programs

And delivers those points in an excellent manner. Each chapter is well written, with tons of examples and detailed descriptions. You can learn how the feature work (although it’s usually a concise introduction, and it’s good to know this feature before). Then you get into use cases, potential pitfalls, annoyances and further external resources. After reading a chapter, you have all the material to validate a given feature, whether that’s useful for your project. I feel that each chapter was read repeatedly by many C++ experts (not only authors) and improved until they were almost perfect.

I like the book because I can read it in any order. If I need some specific topic, I can open the given chapter and start reading. The book is a detailed reference guide. If you like, you can read it from cover to cover, but you can also skip some parts and focus on the most important aspects for you at a given time. Additionally, there are tons of cross-references so that you can follow from one chapter to another in a logical order.

As for the examples, they vary in form and size. There are simple and easy-to-understand code samples that present the syntax or some corner cases for a basic feature description. But then there are also examples almost taken from some real-life projects that illustrate typical use cases, nuances, or issues. Having such practical samples improves the applicability of the feature as you can refer to something “real” and not artificial.

I cannot really say what I didn’t like. I definitely won’t argue with the list of selected features, as the list is well prepared, described, and “scored” according to the authors’ metrics. I agree that most Modern C++ features fall into the “grey” area. They are super handy but might have some flaws and inconveniences.

I could probably complain about one thing: why only C++11/14… and only the language features?? :)

Still, the book doesn’t leave with “old stuff”. The authors paid attention and, whenever possible, gave notes about C++17/C++20 changes. So you’re lost, and you can use those features in the current C++20, state-of-the-art guidelines.

And when the time comes, we’ll hopefully get another volume on C++17 and C++20. But let’s wait till those super recent features get more experience and usage from the community and large companies.

From the “physical” point of view, the book is large, and I might want to have a hardcover rather than a soft one. 1300 pages sometimes feel fragile, and it’s already heavy, so a hardcover wouldn’t increase the weight that much (but it could increase the price by a lot, maybe…). The comments in the code samples are printed in light font color, so this could be a bit improved.

Summary  

Final mark: 5+/5

Pros:

  • No need to read all at once; pick a chapter that you need, in any order, excellent as a quick-reference guide
  • A super valuable content for trainers, technical managers, and leaders who need a guide for Modern C++
  • Deep and detailed,
  • Lots of good examples, from simple (for general understanding) to practical (almost taken from the real projects)
  • Additional information about C++17 and C++20 related changes
  • Bonus chapters or sections from Andrei Alexandrescu, Sean Parent, Nail Douglass
  • Great glossary at the end of the book, around 60 pages!

Cons:

  • It’s heavy and hard to carry around :)
  • Pages feel very thin, and there’s no hardcover, so it’s best to keep it in a safe place.
  • Comments in the code snippets have too bright font color, but they look fine in the PDF version.

Get the book here: Embracing Modern C++ Safely 1st Edition @Amazon