C++ Initialization Story
Learn all about initialization in Modern C++!
Print version @Amazon
C++ Initialization Story @Leanpub
On this website you can find core information about the book, notes and errata.
Github and examples:
See all the code at:
https://github.com/fenbf/cppinitbook_public/tree/main/examples
Book Versions
- 20th June 2022 - The first public version! 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 andnoexcept
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 - heavily updated and completed “Non-regular data members” chapter,
constinit
andthread_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.
- 4th February 2023 - updated layout, wording improvements, fixes, improve consistency of examples.
- 27th February 2023 - the first edition in Print!
- 13th April 2023 - the final quiz questions 6th and 8th clarification, small fixes.
Errata
Report a new issue at https://github.com/fenbf/cppinitbook_public/issues
- Page 108, section “Lifetime extension, references, and loops”, last example. Is
for (auto temp getVec(); auto& i : temp[1])
but should befor (auto temp = getVec(); auto& i : temp[1])
. - Chapter: The Final Quiz And Exercises: Question 6th - the code should be
struct C {
notclass C
- otherwise the question might not be clear enough, asclass
makes all members (including) the constructorprivate
. - Chapter: The Final Quiz And Exercises: Question 8th - the code should be
struct Point { int x; int y; };
notclass Point
- otherwise the question might not be clear enough, asclass
makes all members (including) the constructorprivate
. - Example Ex 1.3. fixed a typo
Line longLone
->Line longLine
, pull request: ch1 ex3 - Section “Defining a class”, page 13: The class above contains three non-static data members:
data_
,checkSum_
, andserverID_
-> should beserverId_
. - Section “Adding constructors to
DataPacket
” page 26,serverId { 0 };
should be `serverId_ { 0 - Page 240, Information box about “Factory with self-registering types…” should be:
My current implementation uses
std::array
, which can be used in constant expressions and doesn’t require dynamic memory allocations. Alternatively, you can also use a static variable returned from a function (similar to Meyers Singleton), and this will also guarantee proper initialization order. Note that usingstd::vector
for the map implementation won’t work in C++20.std::vector
can be used at compile-time, but memory allocations from compile-time are not transient.