I’m happy to announce that my new book on C++ Initialization is published!
Have a look at the background story and how to get it.
Note: Initially, the book was called “Data Member Initialization in Modern C++”, but in September 2022, I updated it heavily and changed the title.
About the book
As for my other books, I used the Leanpub platform for the PDF/Epub generation. Here’s the book’s website:
And also have a look at Goodreads: C++ Initialization Story by Bartłomiej Filipek and leave youre review.
Why I wrote the book?
Initialization in C++ is a hot topic. With each C++ Standard, we’ll get some updates to the syntax, rules, and new capabilities. With this book, I wanted to clarify the confusion and make it simpler to reason about Modern C++ code.
Have I succeeded?
It’s C++, and you’ll see many opinions, special cases, and even contradicting examples. Yet, I’m happy with my content, and it should certainly improve your knowledge and expand your views.
The goal is to equip you with the following knowledge:
- Explain rules about object initialization, including regular variables, data members, and non-local objects.
- How to implement special member functions (constructors, destructors, copy/move operations) and when they are helpful.
- How to efficiently initialize non-static data members using C++11 features like non-static data member initialization, inheriting, and delegating constructors.
- How to streamline working with static variables and static data members with inline variables from C++17.
- How to work with container-like members, non-copyable data members (like
constdata members) or move-able only data members, or even lambdas.
- What is an aggregate, and how to create such objects with designated initializers from C++20.
The structure of the book
The book contains 14 chapters in the following structure:
- Chapters 1 to 5 create a foundation for the rest of the book. They cover basic initialization rules, constructors, destructors, and the basics of data members.
- Chapter 6 is a short quiz on constructors. You can check your knowledge from the first “part” of the book.
- Chapter 7 (in progress): Type deduction.
- Chapter 8 describes Non-static Data Member Initialization (NSDMI), a powerful feature from C++11 that improves how we work with data members. At the end of the chapter, you can solve a few exercises.
- Chapter 9 discusses how to initialize container-like data members.
- Chapter 10 contains information about non-regular data members and how to handle them in a class. You’ll learn about
unique_ptras a data member, and references.
- Chapter 11 describes static non-local variables, static objects, various storage duration options,
inlinevariables from C++17 and
- Chapter 12 moves to C++20 and describes Designated Initializers, a handy feature based on similar thing from the C language.
- Chapter 13 shows various techniques like passing strings into constructors, strong typing, CRTP class counter, Copy and swap idiom, and more.
- Chapter 14 is the final quiz with questions from the whole book.
And there are two appendices:
- Appendix A - a handy guide about rules for compiler-generated special member functions.
- Appendix B - answers to quizzes and exercises.
In total it’s 213 pages and growing!
Background for the book
Long story short: a few years ago, I wrote an article about NSDMI and got curious about how things work for data members. Last year I created a mini-course on the Educative platform. Starting this year, I wanted to bring the PDF version… and it looks like I discovered and described even more stuff :)
Here’s the initial article from 2015: Non Static Data Members Initialization - C++ Stories. And the course at Educative: Initializing Data Members: From C++11 till C++20.
Initially, after converting the lessons into a pdf, I got something around 60 pages. But then I realized it would be better to describe things with more examples and use cases. After digging, investigating, talking to various people… I ended up with 100 pages and now (Sept 2022) with 213 pages :) While the page count is not the only factor for the book, I believe it corresponds to the quality and practical knowledge I put inside.
As a new experiment, I put quizzes and exercises so that you can practice and check your knowledge. There’s no need to set up any coding environment for those tasks, as you can just use Compiler Explorer links and run all the code in the browser. This also works for 70+ examples you can find across the book - open the link and see how the code works.
Updates and plans
The book is 90% ready, and a few sections are still missing. I hope to complete them in
July and August. If everything goes fine, then I’ll try to prepare the paper-back version in Fall.
I hope to update to make further updates in October in November and prepare the paper back version in December. Currently, the price is set to the minimum price available at Leanpub, but when the book is ready, it will go up.
Currently, the price is set to 9.99$ at Leanpub, but when the book is ready, it will go up. Probably to around 12$.
- Use this coupon code to buy it for 8.99$ (previous minimal price): It’s available till the 16th October: https://leanpub.com/cppinitbook/c/oldsummerprice
This book wouldn’t be possible without valuable input from many C++ experts and friends.
I especially would like to thank to the following people:
- JFT (John Taylor),
- Mariusz Jaskółka,
- Florin Chertes (see LinkedIn),
- Konrad Jaśkowiec,
- Professor Boguslaw Cyganek (see his profile at AGH university page),
- Dawid Pilarski (see his blog @panicsoftware.com)
- Javier Estrada (see his blog at javierestrada.blog),
- Jonathan Boccara (from fluentcpp.com/),
- Andreas Fertig (see his blog @andreasfertig).
- Peter Sommerlad (see his website and training info at sommerlad.ch/),
- Timur Doumler (see his website at timur.audio/ and his Twitter).
They spent a lot of time finding even little things that could be improved and extended.
With all of the help from those kind people, the book quality got better and better!
Asking for help and review:
Please join the reviewer list by providing me with your suggestions and improvements. I’m happy to update the book. Just send an email to bartek AT cppstories.com.
If you already have the book, please add your review at: C++ Initialization Story by Bartłomiej Filipek | Goodreads.
- 20th June 2022 - The first public version! The books is almost done. Missing parts: some sections in 10. Containers as Data Members, some sections in 11. Non-regular Data Members.
- 22nd June 2022 - new sections on NSDMI, direct init and parens, more about inheriting constructors, link to GoodReads, wording, hotfixes.
- 24th June 2022 - updated the “copy and move constructor” chapter, typos and small wording improvements.
- 16th July 2022 - Containers as Data Members chapter rewritten,
noexceptmove operations advantages in the move constructor section, wording, fixes, layout.
- 13th September 2022 - changed title to “C++ Initialization Story”, adapted book structure, rewritten “Non-local objects” chapter (previously only on
inlinevariables), new extracted chapter on Techniques, new section on CRTP.
How to Get the Book?
Here’s the link to the book @Leanpub:
Leanpub offers a 60-day refund period!
And several options to get it cheaper:
Buy together with my C++ Lambda Story ebook: Buy C++ Lambda Story and C++ Initialization Story, 14.99$ instead of 29.98$
Support me on Patreon Become a Patron - starting from the 6$ tier, you’ll get the C++ Lambda Story ebook and the new book on Data Members. Plus a discount for C++17 in Detail. See all benefits here: Premium Content! - C++ Stories
You can also download a free sample that contains almost 60 pages (available from the Leanpub page)!