{"id":3249,"date":"2023-11-06T19:11:36","date_gmt":"2023-11-06T13:41:36","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3249"},"modified":"2023-11-07T04:55:39","modified_gmt":"2023-11-06T23:25:39","slug":"book-review-c-initialization-story","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/06\/book-review-c-initialization-story\/","title":{"rendered":"Book Review: C++ Initialization Story"},"content":{"rendered":"<p><span style=\"color: #0000ff;\"><em><strong>Title:<\/strong><\/em><\/span> C++ Initialization Story &#8211; A Guide Through All Initialization Options and Related C++ Areas<br \/>\n<span style=\"color: #0000ff;\"><em><strong>Author:<\/strong><\/em><\/span> Bart\u0142omiej Filipek<br \/>\n<span style=\"color: #0000ff;\"><em><strong>Publisher:<\/strong><\/em><\/span> Leanpub<br \/>\n<span style=\"color: #0000ff;\"><em><strong>Year:<\/strong><\/em><\/span> 2023<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Book-cover.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3250\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/06\/book-review-c-initialization-story\/book-cover-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Book-cover.png\" data-orig-size=\"683,888\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Book-cover.png\" class=\" wp-image-3250 alignleft\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Book-cover.png?resize=185%2C241&#038;ssl=1\" alt=\"\" width=\"185\" height=\"241\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Book-cover.png?resize=231%2C300&amp;ssl=1 231w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Book-cover.png?w=683&amp;ssl=1 683w\" sizes=\"(max-width: 185px) 100vw, 185px\" \/><\/a><\/p>\n<p>Can you believe that there is a language where <em><strong>\u201cinitialization\u201d<\/strong><\/em> itself involves many subtleties and a whole book can be devoted to this topic? Yes, modern <em><strong>C++<\/strong><\/em> is such a language!<\/p>\n<p>While many have argued that <em><strong>C++<\/strong><\/em> 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!<\/p>\n<p>The present book <em><strong>\u201cC++ Initialization Story\u201d<\/strong><\/em> by <em><strong>Bart\u0142omiej Filipek<\/strong><\/em> does a fantastic job of teaching the different aspects of <em><strong>initialization<\/strong><\/em> in <em><strong>C++<\/strong><\/em> (up to <em><strong>C++20<\/strong><\/em>).<\/p>\n<p>The book starts off, in <em><strong>Chapter 1<\/strong><\/em>, with an overview of initialization of local variables including aggregate objects and arrays, pointing out along the way how <em><strong>declaration<\/strong><\/em> is different from <em><strong>definition<\/strong><\/em>.<\/p>\n<p>The next chapter covers the importance of a <em><strong>constructor<\/strong><\/em>, including the <em><strong>explicit<\/strong><\/em> type. The roles of <em><strong>default<\/strong><\/em> and <em><strong>deleted<\/strong><\/em> constructors are discussed in some detail. <em><strong>\u201cUniform initialization\u201d<\/strong><\/em> is a powerful idea introduced in newer versions of <em><strong>C++<\/strong> <\/em>and the author covers it here with examples. He also explains one of the subtler uses of a constructor &#8211; to facilitate implicit and explicit <em><strong>type conversion<\/strong><\/em>.<\/p>\n<p><em><strong>Chapter 3<\/strong><\/em> takes us through <em><strong>Copy<\/strong><\/em> and <em><strong>Move<\/strong><\/em> constructors. While the former is used where special copy semantics are required, the latter is typically used for resource optimization. <em><strong>Chapter 4<\/strong> <\/em>discusses how initialization code can be shared across multiple constructors through <em><strong>delegating<\/strong><\/em> constructors and how constructors can also be inherited in a hierarchy.<\/p>\n<p>Although this book is about <em><strong>initialization<\/strong><\/em>, understanding object construction is not complete without understanding object <em><strong>destruction<\/strong><\/em>. <em><strong>Chapter 5<\/strong><\/em> is a gentle introduction to the role of <em><strong>destructors<\/strong><\/em> in <em><strong>C++<\/strong><\/em> and the author does a nice job here by even explaining <em><strong>virtual destructors<\/strong><\/em> that are sometimes needed when class hierarchies are involved.<\/p>\n<p><em><strong>Chapter 6<\/strong><\/em> deals with compile-time automatic type deduction during initialization through <em><strong>auto<\/strong><\/em> and <em><strong>decltype<\/strong><\/em> keywords. In modern <em><strong>C++<\/strong><\/em>, it is strongly recommended to use <em><strong>auto<\/strong><\/em> wherever possible since it promotes readability, among others. It even works nicely with <em><strong>templates<\/strong><\/em>.<\/p>\n<p><em><strong>Chapter 8<\/strong><\/em> explains non-static data member initialization (<em><strong>NSDMI<\/strong><\/em>) 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 <em><strong>copy<\/strong><\/em> and <em><strong>move<\/strong><\/em> constructors are elaborated in this context. I liked the crisp summary at the end of the chapter enumerating the advantages and limitations of <em><strong>NSDMI<\/strong><\/em>. For example, using <em><strong>auto<\/strong><\/em> for automatic type deduction of non-static data members is disallowed.<\/p>\n<p>Using <em><strong>std::initializer_list<\/strong><\/em> in the context of constructors and various containers (<em><strong>arrays<\/strong><\/em>, <em><strong>lists<\/strong><\/em>, <em><strong>vectors<\/strong><\/em>, <em><strong>maps<\/strong><\/em>, etc.) is discussed in <em><strong>Chapter 9<\/strong><\/em>. The author also warns about additional copies that might be created in these cases.<\/p>\n<p>How to handle certain special categories of data members such as <em><strong>const<\/strong><\/em> qualified members, <em><strong>Pointers<\/strong><\/em>, <em><strong>References<\/strong><\/em>, and <em><strong>Smart pointers<\/strong><\/em> is the focus of <em><strong>Chapter 10<\/strong><\/em>.<\/p>\n<p><em><strong>Chapter 11<\/strong><\/em> covers non-local objects, i.e., globals, static data members, heap objects, and thread-local objects. There is also a brief discussion of <em><strong>constinit<\/strong><\/em> qualifier (available from <em><strong>C++20<\/strong><\/em>).<\/p>\n<p><em><strong>Chapter 12<\/strong><\/em> goes into the finer details of initializing aggregate data types, and how <em><strong>designated initializers<\/strong><\/em> come in handy here.<\/p>\n<p><em><strong>Chapter 13<\/strong><\/em> will be useful even to programmers with prior experience in <em><strong>C++<\/strong><\/em>. Here the author builds upon the various concepts covered in the earlier chapters and describes some of the widely used techniques such as <em><strong>\u201cCopy and Swap Idiom\u201d<\/strong><\/em>, use of <em><strong>emplace<\/strong> <\/em>for avoiding unnecessary copies, the <em><strong>\u201cCuriously Recurring Template Pattern (CRTP)\u201d<\/strong><\/em>, and the <em><strong>\u201cSingleton\u201d<\/strong><\/em> pattern. I really enjoyed reading this chapter.<\/p>\n<p><em><strong>Chapters 7<\/strong><\/em> and <em><strong>14<\/strong><\/em> contain Quiz to test the reader\u2019s basic understanding of the topics covered.<\/p>\n<p>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 <a href=\"https:\/\/godbolt.org\/\" target=\"_blank\" rel=\"noopener\"><em><strong>Compiler Explorer<\/strong><\/em><\/a>.<\/p>\n<p>If you would like to develop a clear understanding of the different nuanced aspects of <em><strong>initialization<\/strong><\/em> in <em><strong>C++<\/strong><\/em>, and along the way learn some good programming tips, then read this book from cover to cover!<\/p>\n<p>Have a nice week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Title: C++ Initialization Story &#8211; A Guide Through All Initialization Options and Related C++ Areas Author: Bart\u0142omiej Filipek Publisher: Leanpub Year: 2023 Can you believe that there is a language where \u201cinitialization\u201d itself involves many subtleties and a whole book can be devoted to this topic? Yes, modern C++ is such a language! While many [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[94,49],"tags":[373],"class_list":["post-3249","post","type-post","status-publish","format-standard","hentry","category-book-review","category-c","tag-c-initialization"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Qp","jetpack-related-posts":[{"id":666,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/10\/21\/c17-initalization-in-selection-statements\/","url_meta":{"origin":3249,"position":0},"title":"C++17: Initialization in Selection Statements","author":"admin","date":"October 21, 2017","format":false,"excerpt":"C++17 enhances if and switch statements with the ability to define\u00a0 variables whose life-time is limited to the corresponding scope. This is in keeping with the general guideline that variables should have a tight scope, i.e.,\u00a0 should be defined as close to the point of use as possible and should\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Sample Class","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/10\/classdefn.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3230,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/","url_meta":{"origin":3249,"position":1},"title":"C++20: \u201cconstinit\u201d Specifier","author":"admin","date":"October 15, 2023","format":false,"excerpt":"The constinit specifier, introduced in C++20, is applied to static variables (global and local static) and thread local variables, with the requirement that they either have a zero initialization or they are initialized with a compile-time constant expression. Here is our first example: Line 17 declares a global constinit variable,\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example-1: Basic Types","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1-300x178.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":3249,"position":2},"title":"C++17: std::variant","author":"admin","date":"November 5, 2017","format":false,"excerpt":"C++17 introduces a new type-safe union in the form of std::variant. At any time, it can store a single value from one of many types. We need to include <variant>\u00a0to use this feature. Let us look at a simple example to get started: First, we define a variant object v1\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1339,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/12\/23\/book-review-real-time-c\/","url_meta":{"origin":3249,"position":3},"title":"Book Review: Real-Time C++","author":"admin","date":"December 23, 2018","format":false,"excerpt":"Title: Real-Time C++: Efficient Object-Oriented and Template Microcontroller Programming Author: Christopher Kormanyos Publisher: Springer-Verlag Year: 2018 (Third Edition) It was the title of this book which caught my attention a few weeks ago and I decided to invest in it. I am not in to real-time software development these days,\u2026","rel":"","context":"In &quot;Book Review&quot;","block_context":{"text":"Book Review","link":"https:\/\/www.rangakrish.com\/index.php\/category\/book-review\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2011,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/06\/stdis_trivial\/","url_meta":{"origin":3249,"position":4},"title":"std::is_trivial","author":"admin","date":"June 6, 2020","format":false,"excerpt":"Today let us try to understand the type trait std::is_trivial<T>. This trait checks if the given type is a trivial type. For a precise definition of what trivial means, please visit the official page. As usual, we will go through three cases: - Primitive types - Classes without inheritance -\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Program Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/output2.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2071,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/08\/01\/elixir-wrapping-lisp-calls-in-a-macro\/","url_meta":{"origin":3249,"position":5},"title":"Elixir: Wrapping Lisp Calls in a Macro","author":"admin","date":"August 1, 2020","format":false,"excerpt":"In the last article, I showed how to make calls to Lisp functions from Elixir. We followed this pattern: 1) Initialize the Lisp environment by calling Lisp.init 2) Call Lisp functions as needed 3) Free the Lisp environment by calling Lisp.quit Using the powerful macro programming capabilities of Elixir, we\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Sample Session","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/Session.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=3249"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3249\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}