Sponsors: KDAB and Whole Tomato Software

19 June 2017

Modern C++ Programming Cookbook Review

Modern C++ Programming

In May 2017 we got one more book about Modern C++! A Few weeks ago I got a copy from Packt Publishing, and today I’d like to write a few words about the book. In short: it’s a very good book! :)

But let’s see what’s inside…

The Book

Modern C++ Programming Cookbook
by Marius Bancila

About Marius: his blog, @mariusbancila

His post about the book being published

source code available at PackPub site

The Structure

The Book is intended for all C++ developers, regardless of their experience. Beginner and intermediate developers will benefit the most from the book in their attempt to become prolific with C++.
Experienced C++ developers, on the other hand, will find a good reference for many C++11, C++14, and C++17 language and library features that may come in handy from time to time.

There are 11 chapters, around 550 pages, and over 100 recipes!

1. Learning Modern Core Language Features

Using auto, type aliases, uniform initialization, scoped enums, and even structured bindings (C++17)

2. Working with Numbers and Strings

Performing conversions, handling numeric types, user defined literals, string_view (C++17)

3. Exploring Functions

Deleted functions, lambdas, map and folds, higher order functions, functional programming

4. Preprocessor and Compilation

Conditional compilation, preprocessor hacks, enable_if (SFINAE), constexpr if (C++17), attributes.

5. Standard Library Containers, Algorithms, and Iterators

Using vector, bitset, algorithms, searching, writing a custom iterator

6. General Purpose Utilities

Time intervals, measuring time, hashing, std::any, std::optional, std::variant (all from C++17), visitors, type traits.

7. Working with Files and Streams

Reading and writing from/to files, serialization of objects, filesystem (C++17)

8. Leveraging Threading and Concurrency

Threads, locking, async invocation, implementing parallel map and fold, tasks, atomics.

9. Robustness and Performance

Exceptions, noexcept, constant expressions, smart pointers, move semantics.

10. Implementing Patterns and Idioms

improving factory patterns (by avoiding if…else statements), pimpl idiom, named parameter idiom, NVI, attorney-client idiom, thread-safe singleton.

11. Exploring Testing Frameworks

Writing tests in Boost.Test, Google Test, Catch

My View

As you can see with the book, we get a lot of useful recipes. What I like from the outset, is that there are topics from C++11, C++14, and even C++17. Thus the book is up to date (even further than the current C++ status!). The author explains clearly what changed between C++ versions. I know how hard it is to pick out all of those little nuances in the standard versions, so it’s a solid advantage of the book.

A few of recipes that caught my attention:

  • Enabling range-based for loops for custom types - very handy if you work with non standard library containers.
  • Creating cooked user-defined literals
  • Using string_view instead of constant string references
  • Using fold expressions to simplify variadic function templates.”
  • Chapters about functional programming
  • Providing metadata to the compiler with attributes
  • Serialization
  • C++17 library features: any, variant, optional and also filesystem.
  • Patterns like attorney-client idiom.
  • Chrono

For example, with chrono I’ve found some beautiful code, take a look:

using namespace std::chrono_literals;
auto d1 = 1h + 23min + 45s; // d1 = 5025s
auto d2 = 3h + 12min + 50s; // d2 = 11570s
if (d1 < d2) { /* do something */ }

Isn’t this clean and expressive? The code is possible thanks to chrono_literals (available since C++14) and User Defined Literals (C++11).

The cookbook style is well suited for ‘modern’ learning when you want to quickly pick a topic and read - without the need to read from beginning to end. Here, depending on your knowledge level and experience you might want to read the whole book, or just choose several recipes. I like such an approach. I am a fan of cookbooks as I’ve reviewed some of them previously (like here,here and here)

With this book, we get a lot of ‘meat’ inside. There’s no theoretic/bloviated chapters, waffling or something like that… you get an actionable recipe that you can use in your code, experiment. Of course, recipes are often connected - in one chapter in particular they go from the most basic up to advanced areas.

I believe it was also quite hard to decide what to include in the book. In theory having ‘all’ recipes for C++ would take like 2000… or 3000 pages. Still, I think the book is well organized, and you get most of the useful stuff from modern C++. Of course, I’d like to ask for more :)

The selection of topics tries to cover all needs. If you need some specific/advanced parts then you can pick up other books like Effective Modern C++, C++ Concurrency in Action, Discovering Modern C++.

Summary

Final mark: 4.5/5

Pros:

  • Clear structure
  • Cookbook style, so read what you need
  • Chapters usually starts with some basic recipes and then increase the level of complexity.
  • Concise examples and details of how it works, so not just functions headers.
  • Modern coding standard, even with C++17 stuff!
  • C++11, C++14, and C++17 - with clear distinction, explanation of what have changeed, etc.
  • It doesn’t have much ‘intro to C++,’ so you can just jump into intermediate topics! It’s not another basic beginner’s book.
  • There are useful ‘tips’ here and there

Cons:

  • A few typos, repetitions, one missing function description
  • Chapter about unit testing frameworks could be shorter, but maybe other devs find it useful.
  • Some recipes are questionable: but that depends on the view/experience. For example: using bitset. And I’d like to see more performance topics in the performance chapter.

Overall, I like the book. With its clear structure and well-written recipes, it’s a good addition to any C++ bookshelf. It’s well suited for the target audience: even if you’re an expert, you’ll get a chance to refresh your knowledge and update it with C++14/C++17 content. And If you’ve just finished some beginner book, you’ll find here topics that will move you forward.

I am impressed how Marius ended up with such a good book, especially as it’s his first one as far as I know. I think the second edition of C++ Cookbook will be just perfect :)

Giveaway

The Giveaway has ended!.
Here are the winners:

  • Rene Aguirre
  • Josh Kelley
  • Cloud Force

Congratulations!

  • What’s you favourite modern C++ area?
  • Do you use modern C++, or you’re stuck with some legacy/old standard?
  • What recipes for modern C++ would you like to read more?
  • What other books you’d suggest for modern C++?

Interested in new blog posts and bonus content? Sign up for my newsletter.

© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.