Since 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…
Important: I have three ebooks for you, read more at the end of the post!
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. The 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, 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 patters (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 in the first place, 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 all of those little nuances in the standard versions, so it’s a solid advantage of the book.
A few of recipes that took my attention:
- Enabling range-based for loops for custom types - very handy if you don’t work only with 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:
usingnamespace 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? Code 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 the beginning to the 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 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 lof 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 - especially in one chapter 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 of needs. If you need some specific/advanced parts then you can pick 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 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 what have changes, etc.
- It doesn’t have much of ‘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 that it’s his first one as I know. I think the second edition of C++ Cookbook will be just perfect :)
Giveaway
Together with Packt Publishing, we have three “Modern C++ Programming Cookbook” ebooks to give!
All you have to do is enter your email/name into this funny giveaway tool below and take part in the game :)
Please note that if you're already on the mailing list it doesn't mean you take part in the giveaway, you have to enter the game 'explicitly'.
Also, If you write a comment, that increases your chances to win! (Be sure to also subscribe to the game tool!)
To help you with a comment idea you can think about the following questions:
- 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++?
We’ll randomly draw 3 winners on July 3rd; it will be announced here in this post and on Twitter. So you have two weeks to take the action :)