Book Review: C++ Initialization Story

Written by on November 6, 2023 in Book Review, C++ with 0 Comments

Title: C++ Initialization Story – A Guide Through All Initialization Options and Related C++ Areas
Author: Bartłomiej Filipek
Publisher: Leanpub
Year: 2023

Can you believe that there is a language where “initialization” itself involves many subtleties and a whole book can be devoted to this topic? Yes, modern C++ is such a language!

While many have argued that C++ is too complex for serious programming, I personally feel (having used it for over 30 years) that this complexity can be mastered in layers and with experience, one can become good at writing efficient programs. The various language features are designed to help a good programmer write great code!

The present book “C++ Initialization Story” by Bartłomiej Filipek does a fantastic job of teaching the different aspects of initialization in C++ (up to C++20).

The book starts off, in Chapter 1, with an overview of initialization of local variables including aggregate objects and arrays, pointing out along the way how declaration is different from definition.

The next chapter covers the importance of a constructor, including the explicit type. The roles of default and deleted constructors are discussed in some detail. “Uniform initialization” is a powerful idea introduced in newer versions of C++ and the author covers it here with examples. He also explains one of the subtler uses of a constructor – to facilitate implicit and explicit type conversion.

Chapter 3 takes us through Copy and Move constructors. While the former is used where special copy semantics are required, the latter is typically used for resource optimization. Chapter 4 discusses how initialization code can be shared across multiple constructors through delegating constructors and how constructors can also be inherited in a hierarchy.

Although this book is about initialization, understanding object construction is not complete without understanding object destruction. Chapter 5 is a gentle introduction to the role of destructors in C++ and the author does a nice job here by even explaining virtual destructors that are sometimes needed when class hierarchies are involved.

Chapter 6 deals with compile-time automatic type deduction during initialization through auto and decltype keywords. In modern C++, it is strongly recommended to use auto wherever possible since it promotes readability, among others. It even works nicely with templates.

Chapter 8 explains non-static data member initialization (NSDMI) in detail. Starting with basic direct initialization of data members (non-static), the author shows how default and other constructors come into the picture as part of object initialization. The roles of copy and move constructors are elaborated in this context. I liked the crisp summary at the end of the chapter enumerating the advantages and limitations of NSDMI. For example, using auto for automatic type deduction of non-static data members is disallowed.

Using std::initializer_list in the context of constructors and various containers (arrays, lists, vectors, maps, etc.) is discussed in Chapter 9. The author also warns about additional copies that might be created in these cases.

How to handle certain special categories of data members such as const qualified members, Pointers, References, and Smart pointers is the focus of Chapter 10.

Chapter 11 covers non-local objects, i.e., globals, static data members, heap objects, and thread-local objects. There is also a brief discussion of constinit qualifier (available from C++20).

Chapter 12 goes into the finer details of initializing aggregate data types, and how designated initializers come in handy here.

Chapter 13 will be useful even to programmers with prior experience in C++. Here the author builds upon the various concepts covered in the earlier chapters and describes some of the widely used techniques such as “Copy and Swap Idiom”, use of emplace for avoiding unnecessary copies, the “Curiously Recurring Template Pattern (CRTP)”, and the “Singleton” pattern. I really enjoyed reading this chapter.

Chapters 7 and 14 contain Quiz to test the reader’s basic understanding of the topics covered.

I appreciate the author for the liberal use of examples throughout the book. This will definitely appeal all audience. The discussed examples can be tried out readily in Compiler Explorer.

If you would like to develop a clear understanding of the different nuanced aspects of initialization in C++, and along the way learn some good programming tips, then read this book from cover to cover!

Have a nice week!



If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *