{"id":600,"date":"2019-09-26T23:24:24","date_gmt":"2019-09-27T06:24:24","guid":{"rendered":"http:\/\/35.243.195.209\/?p=600"},"modified":"2019-09-26T23:28:39","modified_gmt":"2019-09-27T06:28:39","slug":"in-class-initialization-errors","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/09\/26\/in-class-initialization-errors\/","title":{"rendered":"In-Class Initialization Errors"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce some common compiling errors I used to see related to in-class initialization: <\/p>\n<ol>\n<li>Error: in-class initialization of static data member xxx of non-literal type;<\/li>\n<li>Error: ISO C++ forbids in-class initialization of non-const static member xxx;<\/li>\n<\/ol>\n<h2>Conclusion<\/h2>\n<p>As usual, I present the correct ways to do in-class initialization at the very beginning. <\/p>\n<pre><code class=\"language-cpp\">\/\/ header.h\nclass Base {\npublic:\n    static int count;\n\n    static char const static_char = &#039;a&#039;;\n    static int const static_int = 1;\n\n    static constexpr std::pair&lt;int, int&gt; static_constexpr_pair = {0, 1};\n\n    static std::string static_var;\n    static std::string const static_const_var;\n    std::string const const_var = &quot;const_var&quot;;\n\n    std::vector&lt;int&gt; const const_vec = {0, 1, 2};\n\n    Base() {\n        count++;\n    }\n};\n\n\/\/ src.cpp\nint Base::count = 0;\n\nstd::string Base::static_var = &quot;static_var&quot;;\nstd::string const Base::static_const_var = &quot;static_const_var&quot;;<\/code><\/pre>\n<p>Some golden rules we can follow are:<\/p>\n<ol>\n<li>For static varibles, do not initialize them Inline;<\/li>\n<li>Always initialize static varibles in the source file;<\/li>\n<li>You can do any kind of inline initialization for non-static variable. <\/li>\n<\/ol>\n<h2>Details<\/h2>\n<h3>Why I can&#8217;t initialize static data members in class?<\/h3>\n<p>You will get &quot;Error: in-class initialization of static data member xxx of non-literal type&quot; in the following codes. <\/p>\n<pre><code class=\"language-cpp\">class Base {\npublic:\n    static std::string static_var = &quot;static_var&quot;;\n    static std::string const static_const_var = &quot;static_const_var&quot;;\n}<\/code><\/pre>\n<p>Bjarne explains this aptly <a href=\"http:\/\/www.stroustrup.com\/bs_faq2.html#in-class\">here<\/a>:<\/p>\n<p>&quot;A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects.&quot;<\/p>\n<h3>Inline initialization of static member variables<\/h3>\n<p>There are a few shortcuts to the above. First, when the static member is a const integral type (which includes char and bool) or a const enum, the static member can be initialized inside the class definition:<\/p>\n<pre><code class=\"language-cpp\">\/\/ header.h\nclass Base {\npublic:\n    static char const static_char = &#039;a&#039;;\n    static int const static_int = 1;\n}<\/code><\/pre>\n<p>The reason is &quot;Static const integers (and enums) can be treated as compile-time constants, so the compiler can replace calls to the const integer with the value itself.&quot;<\/p>\n<p>Second, as of C++11, static constexpr members of any type that supports constexpr initialization can be initialized inside the class definition:<\/p>\n<pre><code class=\"language-cpp\">class Base {\npublic:\n    static constexpr std::pair&lt;int, int&gt; static_constexpr_pair = {0, 1};\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce some common compiling errors I used to see related to in-class initialization: Error: in-class initialization of static data member xxx of non-literal type; Error: ISO C++ forbids in-class initialization of non-const static member xxx; Conclusion As usual, I present the correct ways to do in-class initialization at the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-600","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=600"}],"version-history":[{"count":4,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/600\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}