I’m happy to announce that my new book on C++ Initialization is published and finished!

Have a look at the background story and how to get it.

Updates: Go to the latest updates from 23rd Dec here, 30 new pages added!

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:


C++ Initialization Story @Leanpub

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.

It may look scary to some people, though…

“what??? 275 pages on just initialization???”

I didn’t just want to write ten pages filled with rules; instead, I tried to present C++ from the perspective of the initialization. And since initialization touches so many aspects of C++… here we are with that page count. Among those rules, you’ll find many essential techniques and general good practices for modern C++.

Learning objectives  

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 const data 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 Type deduction: auto, decltype, AAA
  • 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 const data members, unique_ptr as a data member, and references.
  • Chapter 11 describes static non-local variables, static objects, various storage duration options, inline variables from C++17 and constinit from C++20.
  • 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 275 pages!

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 (Dec 2022) with 275 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.

Source code:

You can see all examples at this GitHub repository:

https://github.com/fenbf/cppinitbook_public

Updates and plans  

Last update: 23rd December:

  • Added a new chapter on Deduction (auto, decltype, AAA)
  • dded new sections in the techniques chapter: in_place construction, Singletons, Factories
  • Improved quiz sections, added new questions and one exercise
  • lots of layout fixes and improvements

Also, have a look at my video describing thread_local:

Plans  

The book is 100% “content” ready!

Now, I want to improve some layout, add index and prepare the print version for Amazon KDP.

Currently, the price is set to the minimum price of 7.99$ available at Leanpub, but when the book is ready, it will go up.

CCurrently, the price is set to 9.99$ at Leanpub, but when the book is ready, it will go up. Probably to around 12$.

Currently the price is set to 15.99$.

But here’s a coupon code valid in December to buy it at the “old” price:

https://leanpub.com/cppinitbook/c/olddecprice

Acknowledgements  

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:

They spent a lot of time finding even little things that could be improved and extended.

Last but not least, I got a lot of feedback and comments from the blog readers, Patreon Discord Server (See @C++Stories @Patreon), and discussions at C++ Polska. Thank you all!

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.

Changelog  

  • 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, noexcept consistency and noexcept move 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 inline variables), new extracted chapter on Techniques, new section on CRTP.
  • 18th November 2022 - added 30 more pages! heavily updated and completed “Non-regular data members”) chapter, constinit and thread_local sections in the “Non-local objects” chapter, filled the “implicit conversion” section in the “Constructors” chapter.
  • 23rd December 2022 - content completed! Added Deduction chapter, filled missing sections in the Techniques chapter. Layout improvements, a few more questions, exercises and fixes.

How to Get the Book?  

Here’s the link to the book @Leanpub:


C++ Initialization Story @Leanpub

Leanpub offers a 60-day refund period!

And several options to get it cheaper:

You can also download a free sample that contains almost 60 pages (available from the Leanpub page)!