{"id":5372,"date":"2018-01-12T18:24:40","date_gmt":"2018-01-12T18:24:40","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-declarations-and-initialisations\/"},"modified":"2023-06-26T11:59:07","modified_gmt":"2023-06-26T11:59:07","slug":"c-core-guidelines-declarations-and-initialisations","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-declarations-and-initialisations\/","title":{"rendered":"C++ Core Guidelines: Declarations and Initialisations"},"content":{"rendered":"<p>Let&#8217;s continue our tour through the rules for expressions and statements in the C++ core guidelines. This post will be about declarations and initializations.&nbsp;<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5368\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/creation-of-man.png\" alt=\"creation of man\" width=\"640\" height=\"298\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/creation-of-man.png 640w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/creation-of-man-300x140.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>To be honest, most of the rules in this post are quite obvious but they often provide one or the other very interesting insight; therefore, I will mainly write in this post about these special aspects. Here are the rules for today:<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-auto\">ES.11: Use&nbsp;<code class=\"highlighter-rouge no-highlight\">auto<\/code>&nbsp;to avoid redundant repetition of type names<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-reuse\">ES.12: Do not reuse names in nested scopes<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-always\">ES.20: Always initialize an object<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-introduce\">ES.21: Don\u2019t introduce a variable (or constant) before you need to use it<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-init\">ES.22: Don\u2019t declare a variable until you have a value to initialize it with<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-list\">ES.23: Prefer the&nbsp;<code class=\"highlighter-rouge no-highlight\">{}<\/code>-initializer syntax<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-list\"><\/a><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-unique\">ES.24: Use a&nbsp;<code class=\"highlighter-rouge no-highlight\">unique_ptr&lt;T&gt;<\/code>&nbsp;to hold pointers<\/a><\/li>\n<\/ul>\n<p>Here are the details.&nbsp;<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-auto\">ES.11: Use&nbsp;<code class=\"highlighter-rouge no-highlight\">auto<\/code>&nbsp;to avoid redundant repetition of type names<\/a><\/h3>\n<p>The example from the guidelines is not promising to me. So, let me give you another one. Changing your code may become a piece of cake if you use <span style=\"font-family: courier new, courier;\">auto<\/span>.&nbsp;<\/p>\n<p>The following example is based on <span style=\"font-family: 'courier new', courier;\">auto<\/span>. You do have not to think about the types; therefore, you can not make an error. This means&nbsp;the type of <span style=\"font-family: 'courier new', courier;\">res<\/span> will be <span style=\"font-family: 'courier new', courier;\">int<\/span> at the end.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">auto<\/span> a <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> b <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> sum <span style=\"color: #555555;\">=<\/span>  a <span style=\"color: #555555;\">*<\/span> b <span style=\"color: #555555;\">*<\/span> <span style=\"color: #ff6600;\">3<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> sum <span style=\"color: #555555;\">+<\/span> <span style=\"color: #ff6600;\">10<\/span>; \r\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(res).name();         <span style=\"color: #0099ff; font-style: italic;\">\/\/ i<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>If you decide to change the literal <span style=\"font-family: 'courier new', courier;\">b<\/span> from <span style=\"font-family: 'courier new', courier;\">int<\/span>&nbsp;to <span style=\"font-family: 'courier new', courier;\">double<\/span> (2) or use in (3) a <span style=\"font-family: 'courier new', courier;\">float<\/span> literal instead of the <span style=\"font-family: 'courier new', courier;\">int<\/span> literal. No problem. It will be automatically handled for you.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">auto<\/span> a <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> b <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10.5<\/span>;                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> sum <span style=\"color: #555555;\">=<\/span> a <span style=\"color: #555555;\">*<\/span> b <span style=\"color: #555555;\">*<\/span> <span style=\"color: #ff6600;\">3<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> sum <span style=\"color: #555555;\">*<\/span> <span style=\"color: #ff6600;\">10<\/span>;  \r\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(res).name();         <span style=\"color: #0099ff; font-style: italic;\">\/\/ d<\/span>\r\n  \r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> a <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> b <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10<\/span>;\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> sum <span style=\"color: #555555;\">=<\/span> a <span style=\"color: #555555;\">*<\/span> b <span style=\"color: #555555;\">*<\/span> <span style=\"color: #ff6600;\">3.1f<\/span>;                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> sum <span style=\"color: #555555;\">*<\/span> <span style=\"color: #ff6600;\">10<\/span>;  \r\nstd<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #006699; font-weight: bold;\">typeid<\/span>(res).name();        <span style=\"color: #0099ff; font-style: italic;\">\/\/ f<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-reuse\">ES.12: Do not reuse names in nested scopes<\/a><\/h3>\n<p>This is one of these quite apparent rules. You should not reuse names in nested scopes for readability and maintenance reasons.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ shadow.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">shadow<\/span>(<span style=\"color: #007788; font-weight: bold;\">bool<\/span> cond){\r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> d <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n  <span style=\"color: #006699; font-weight: bold;\">if<\/span> (cond){\r\n    d <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\r\n  }\r\n  <span style=\"color: #006699; font-weight: bold;\">else<\/span> {\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> d <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;\r\n    d <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">3<\/span>;\r\n  }\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> d;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> shadow(<span style=\"color: #336666;\">true<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;      \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> shadow(<span style=\"color: #336666;\">false<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;     \r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>What will be the output of the program? Confused by the d&#8217;s? Here is the result.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5369\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowD.png\" alt=\"shadowD\" width=\"350\" height=\"181\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowD.png 421w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowD-300x155.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>This was easy! Right? But the same phenomenon is quite surprising in class hierarchies.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ shadowClass.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Base{\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> shadow(std<span style=\"color: #555555;\">::<\/span>string){                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Base::shadow\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;       \r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Derived<span style=\"color: #555555;\">:<\/span> Base{\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">shadow<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>){                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3<\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Derived::shadow\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;    \r\n    }\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    Derived derived;\r\n    \r\n    derived.shadow(std<span style=\"color: #555555;\">::<\/span>string{});                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\n    derived.shadow(<span style=\"color: #007788; font-weight: bold;\">int<\/span>{});                              \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Both structs <span style=\"font-family: 'courier new', courier;\">Base<\/span> and <span style=\"font-family: 'courier new', courier;\">Derived<\/span> have a method <span style=\"font-family: 'courier new', courier;\">shadow<\/span>. The one in the base accepts a <span style=\"font-family: 'courier new', courier;\">std:<\/span>:string&nbsp;(2) and the other one an <span style=\"font-family: 'courier new', courier;\">int<\/span>&nbsp;(3). When I invoke the object <span style=\"font-family: 'courier new', courier;\">derived<\/span> with a default-constructed&nbsp;<span style=\"font-family: 'courier new', courier;\">std::string <\/span>(1), I may assume that the base version will be called. Wrong! Because the method&nbsp;<span style=\"font-family: 'courier new', courier;\">shadow<\/span> is implemented in the class <span style=\"font-family: 'courier new', courier;\">Derived<\/span>, the methods of the base class will not be considered during name resolution. Here is the output of my Gcc.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5370\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowClass.png\" alt=\"shadowClass\" width=\"700\" height=\"172\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowClass.png 1181w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowClass-300x74.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowClass-1024x251.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowClass-768x189.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>To fix this issue, <span style=\"font-family: 'courier new', courier;\">shadow<\/span> must be known to&nbsp;<span style=\"font-family: 'courier new', courier;\">Derived<\/span>.&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">struct<\/span> Derived<span style=\"color: #555555;\">:<\/span> Base{\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> Base<span style=\"color: #555555;\">::<\/span>shadow;              <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">shadow<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Derived::shadow\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;   \r\n    }\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>You have to put a <span style=\"font-family: 'courier new', courier;\">Base::shadow<\/span> (1) into <span style=\"font-family: 'courier new', courier;\">Derived.<\/span> Now the program behaves as expected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5371\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowClassFixed.png\" alt=\"shadowClassFixed\" width=\"450\" height=\"176\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowClassFixed.png 655w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/shadowClassFixed-300x117.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-always\">ES.20: Always initialize an object<\/a><\/h3>\n<p>The rules on which object will be initialized or not are pretty tricky to get right in C++. Here is a simple example.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">struct<\/span> T1 {};\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T2<\/span>{\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    T2() {} \r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> n;                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n  <span style=\"color: #007788; font-weight: bold;\">int<\/span> n2;              <span style=\"color: #0099ff; font-style: italic;\">\/\/ ERROR<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>string s;       <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n  T1 t1;               <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK<\/span>\r\n  T2 t2;               <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK                  <\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>n is a global variable, which will be initialized to 0. This will not hold for n2 because it is a local variable and will not be initialized. But they will be initialized if you use a user-defined type such as std::string, T1, or T2 in a local scope.<\/p>\n<p>If that is too difficult for you, I have a simple fix. Use <span style=\"font-family: 'courier new', courier;\">auto.<\/span>&nbsp;Now, you can not forget to initialize the variable. The compiler will check this.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">struct<\/span> T1 {};\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">T2<\/span>{\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    T2() {}\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> n2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> s <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"\"<\/span>s;      \r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> t1 <span style=\"color: #555555;\">=<\/span> T1();               \r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> t2 <span style=\"color: #555555;\">=<\/span> T2();\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-introduce\">ES.21: Don\u2019t introduce a variable (or constant) before you need to use it<\/a><\/h3>\n<p>I think&nbsp;this is trivial. We program C++,&nbsp;not C.<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-init\">ES.22: Don\u2019t declare a variable until you have a value to initialize it with<\/a><\/h3>\n<p>You may have a so-called used-before-set error if you don&#8217;t follow this rule. Have a look at the guidelines.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">int var;  \r\n\r\n<span style=\"color: #006699; font-weight: bold;\">if<\/span> (cond)      <span style=\"color: #0099ff; font-style: italic;\">\/\/ some non-trivial condition<\/span>\r\n    Set(<span style=\"color: #555555;\">&amp;<\/span>var);\r\n<span style=\"color: #006699; font-weight: bold;\">else<\/span> <span style=\"color: #cc00ff;\">if<\/span> (cond2 <span style=\"color: #555555;\">||<\/span> <span style=\"color: #555555;\">!<\/span>cond3) {\r\n    var <span style=\"color: #555555;\">=<\/span> Set2(<span style=\"color: #ff6600;\">3.14<\/span>);\r\n}\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ use var<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Do you know if one of the conditions holds? If not, <span style=\"font-family: 'courier new', courier;\">var<\/span> as a local built-in variable is not initialized.<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-list\">ES.23: Prefer the&nbsp;<code class=\"highlighter-rouge no-highlight\">{}<\/code>-initializer syntax<\/a><\/h3>\n<p>There are a lot of reasons for using {}-initialization:<\/p>\n<ul>\n<li>always applicable<\/li>\n<li>overcomes the most vexing parse<\/li>\n<li>prevents narrowing<\/li>\n<\/ul>\n<p>You have to keep a particular rule in mind. If you combine <span style=\"font-family: courier new, courier;\">auto <\/span>with an {}-initialization, you will get an&nbsp;<span style=\"font-family: 'courier new', courier;\">std::initializer_list <\/span>in C++14 but not in C++17.&nbsp;<\/p>\n<p>For all the details, read my previous post to <a href=\"https:\/\/www.modernescpp.com\/index.php\/initialization\">{}-Initialisation<\/a>.<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-unique\">ES.24: Use a&nbsp;<code class=\"highlighter-rouge no-highlight\">unique_ptr&lt;T&gt;<\/code>&nbsp;to hold pointers<\/a><\/h3>\n<p>I will make it short. A <span style=\"font-family: 'courier new', courier;\">std::unique_ptr&lt;T&gt;<\/span> is as efficient as a raw pointer by design but has a great added value: it takes care of its resource. This means: don&#8217;t use a raw pointer. If you are curious about the details of <span style=\"font-family: 'courier new', courier;\">std::unique_ptr<\/span>, read my two posts to <a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/unique-ptr\">std::unqiue_ptr<\/a>.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>We are not done with the rules for declarations in C++. The&nbsp;remaining will follow in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-more-rules-for-declarations\">next post<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s continue our tour through the rules for expressions and statements in the C++ core guidelines. This post will be about declarations and initializations.&nbsp;<\/p>\n","protected":false},"author":21,"featured_media":5368,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[497,460],"class_list":["post-5372","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-declarations","tag-initialization"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5372"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5372\/revisions"}],"predecessor-version":[{"id":6844,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5372\/revisions\/6844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5368"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}