{"id":4972,"date":"2016-10-06T20:07:00","date_gmt":"2016-10-06T20:07:00","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/automatically-inititialized\/"},"modified":"2023-06-26T12:40:17","modified_gmt":"2023-06-26T12:40:17","slug":"automatically-inititialized","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/automatically-inititialized\/","title":{"rendered":"auto-matically inititialized"},"content":{"rendered":"<p>Probably the most frequently used feature of C++11 is <span style=\"font-family: courier new,courier;\">auto<\/span>. Thanks to auto, the compiler determines the type of a variable from its initializer. But what is the point of safety-critical software?&nbsp;<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2>The facts about <span style=\"font-family: courier new,courier;\">auto<\/span><\/h2>\n<p>Automatic type deduction with auto is exceptionally convenient. First, you save a lot of unnecessary typing, particularly with challenging template expressions; second, the compiler is never wrong &#8211; in opposition to the programmer.&nbsp;<\/p>\n<p>I compare, in the following listing, the explicit and the deduced types.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">#include &lt;vector&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> myAdd(<span style=\"color: #2b91af;\">int<\/span> a,<span style=\"color: #2b91af;\">int<\/span> b){ <span style=\"color: #0000ff;\">return<\/span> a+b; }\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  <span style=\"color: #008000;\">\/\/ define an int-value<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> i= 5;                                  <span style=\"color: #008000;\">\/\/ explicit<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> i1= 5;                                <span style=\"color: #008000;\">\/\/ auto<\/span>\r\n \r\n  <span style=\"color: #008000;\">\/\/ define a reference to an int<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span>&amp; b= i;                                 <span style=\"color: #008000;\">\/\/ explicit<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span>&amp; b1= i;                               <span style=\"color: #008000;\">\/\/ auto<\/span>\r\n \r\n  <span style=\"color: #008000;\">\/\/ define a pointer to a function<\/span>\r\n  <span style=\"color: #2b91af;\">int<\/span> (*add)(<span style=\"color: #2b91af;\">int<\/span>,<span style=\"color: #2b91af;\">int<\/span>)= myAdd;               <span style=\"color: #008000;\">\/\/ explicit<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> add1= myAdd;                         <span style=\"color: #008000;\">\/\/ auto<\/span>\r\n  \r\n  <span style=\"color: #008000;\">\/\/ iterate through a vector<\/span>\r\n  std::vector&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; vec;\r\n  <span style=\"color: #0000ff;\">for<\/span> (std::vector&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;::iterator it= vec.begin(); it != vec.end(); ++it){} \r\n  <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span> it1= vec.begin(); it1 != vec.end(); ++it1) {}\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The compiler uses the rules for <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/template_argument_deduction\">template argument deduction <\/a>to get the variable type. Therefore, the outer&nbsp;<span style=\"font-family: courier new,courier;\">const <\/span>or <span style=\"font-family: courier new,courier;\">volatile<\/span> qualifier and references are removed. The following example shows this behavior for constant and references.&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #2b91af;\">int<\/span> main(){\r\n  \r\n  <span style=\"color: #2b91af;\">int<\/span> i= 2011;\r\n  <span style=\"color: #0000ff;\">const<\/span> <span style=\"color: #2b91af;\">int<\/span> i2= 2014;\r\n  <span style=\"color: #0000ff;\">const<\/span> <span style=\"color: #2b91af;\">int<\/span>&amp; i3= i2;\r\n  \r\n  <span style=\"color: #0000ff;\">auto<\/span> a2= i2;     <span style=\"color: #008000;\">\/\/ int<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> a3= i3;     <span style=\"color: #008000;\">\/\/ int<\/span>\r\n  \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>But how can I be sure that<span style=\"font-family: courier new,courier;\"> a2<\/span> or <span style=\"font-family: courier new,courier;\">a<\/span>3 is of type <span style=\"font-family: courier new,courier;\">int<\/span>, although I used a variable of type<span style=\"font-family: courier new,courier;\"> const int<\/span> or <span style=\"font-family: courier new,courier;\">const int&amp;<\/span> to initialize them? Sometimes I deduce it wrong. The answer is simple. The compiler knows the truth. The only declared class template GetType helps me a lot.&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">typename<\/span> T&gt;\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">GetType<\/span>; \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The compiler will immediately complain if I use only the declared class template. The definition is missing. That is the characteristic I need. The compiler tells me exactly the type of class template, which can not be instantiated. At first, to the extended source code. I disabled the following source code the try to instantiate the only declared class template.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">#include &lt;get_type.hpp&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n  \r\n  <span style=\"color: #2b91af;\">int<\/span> i= 2011;\r\n  <span style=\"color: #0000ff;\">const<\/span> <span style=\"color: #2b91af;\">int<\/span> i2= 2014;\r\n  <span style=\"color: #008000;\">\/\/ GetType&lt;decltype(i2)&gt; myType;<\/span>\r\n  <span style=\"color: #0000ff;\">const<\/span> <span style=\"color: #2b91af;\">int<\/span>&amp; i3= i2;\r\n  <span style=\"color: #008000;\">\/\/ GetType&lt;decltype(i3)&gt; myType;<\/span>\r\n  \r\n  <span style=\"color: #0000ff;\">auto<\/span> a2= i2; \r\n  <span style=\"color: #008000;\">\/\/ GetType&lt;decltype(a2)&gt; myType;<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> a3= i3;\r\n  <span style=\"color: #008000;\">\/\/ GetType&lt;decltype(a3)&gt; myType;<\/span>\r\n  \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The GetType call in lines 7,9, 12, and 14 uses the specifier <span style=\"font-family: courier new,courier;\">decltype,<\/span> which gives you the exact type of the declared variable. The rest is only hard work. I successively commented on each <span style=\"font-family: courier new,courier;\">GetType<\/span> expression. A deep look into the error messages of the g++ compilers is very interesting.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4968\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/autoGetType.png\" alt=\"autoGetType\" width=\"800\" height=\"364\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/autoGetType.png 907w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/autoGetType-300x137.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/autoGetType-768x350.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The critical expressions of the error message have a red line. Impressed? But once more. What is the point of safety-critical software?<\/p>\n<\/p>\n<h2>Initialize me!<\/h2>\n<p><span style=\"font-family: courier new,courier;\">auto<\/span> determines its type from an&nbsp;initializer. That means. Without an initializer, there is no type and, therefore, no variable. To say it positively. The compiler takes care that each type is initialized. That is a nice side effect of <span style=\"font-family: courier new,courier;\">auto<\/span> that is mentioned too rarely.&nbsp;<\/p>\n<p>Whether you forgot to initialize a variable or didn&#8217;t make it because of a wrong understanding of the language makes no difference. The result is simply the same: undefined behavior. With <span style=\"font-family: courier new,courier;\">auto<\/span> you can overcome these nasty errors. Be honest. Do you know all rules for the initialization of a variable? If yes, congratulations. If not, read the article&#8217;s <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/default_initialization\">default initialization<\/a> and all referenced articles in this article. I have no clue why they used the following statement: &#8220;objects with automatic storage duration (and their subobjects) are initialized to indeterminate values&#8221;. This formulation causes more harm than good. Local variables will not be default initialized.<\/p>\n<p>I modified the second program of <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/default_initialization\">default initialization <\/a>to make the undefined behavior more obvious.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ init.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&gt;<\/span>\r\n \r\n<span style=\"color: #0000ff;\">struct<\/span> T1 {};\r\n \r\n<span style=\"color: #0000ff;\">struct<\/span> T2{\r\n    <span style=\"color: #2b91af;\">int<\/span> mem;     <span style=\"color: #008000;\">\/\/ Not ok: indeterminate value<\/span>\r\n public:\r\n    T2() {} \r\n};\r\n \r\n<span style=\"color: #2b91af;\">int<\/span> n;          <span style=\"color: #008000;\">\/\/  ok: initialized to 0<\/span>\r\n \r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #2b91af;\">int<\/span> n;               <span style=\"color: #008000;\">\/\/ Not ok: indeterminate value<\/span>\r\n  std::string s;       <span style=\"color: #008000;\">\/\/ ok: Invocation of the default constructor; initialized to \"\" <\/span>\r\n  T1 t1;               <span style=\"color: #008000;\">\/\/ ok: Invocation of the default constructor <\/span>\r\n  T2 t2;               <span style=\"color: #008000;\">\/\/ ok: Invocation of the default constructor<\/span>\r\n    \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"::n \"<\/span> &lt;&lt; ::n &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"n: \"<\/span> &lt;&lt; n &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"s: \"<\/span> &lt;&lt; s &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"T2().mem: \"<\/span> &lt;&lt; T2().mem &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n                      \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>At first, to the scope resolutions operator<span style=\"font-family: courier new,courier;\">::<\/span> in line 25. <span style=\"font-family: courier new,courier;\">:<\/span>: addresses the global scope. In our case, the variable n in line 14. Curiously enough, the automatic variable<span style=\"font-family: courier new,courier;\"> n<\/span> in line 25 has the value<span style=\"font-family: courier new,courier;\"> 0. n<\/span> has an undefined value, and therefore the program has undefined behavior. That will not hold for the variable <span style=\"font-family: courier new,courier;\">mem<\/span> of the class <span style=\"font-family: courier new,courier;\">T2<\/span>. <span style=\"font-family: courier new,courier;\">mem<\/span> returns an undefined value.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4969\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/init.png\" alt=\"init\" width=\"357\" height=\"234\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/init.png 357w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/init-300x197.png 300w\" sizes=\"auto, (max-width: 357px) 100vw, 357px\" \/><\/p>\n<p>Now, I rewrite the program with the help of <span style=\"font-family: courier new,courier;\">auto<\/span>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ initAuto.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&gt;<\/span>\r\n \r\n<span style=\"color: #0000ff;\">struct<\/span> T1 {};\r\n \r\n<span style=\"color: #0000ff;\">struct<\/span> T2{\r\n    <span style=\"color: #2b91af;\">int<\/span> mem= 0;  <span style=\"color: #008000;\">\/\/ auto mem= 0 is an error<\/span>\r\n public:\r\n    T2() {}\r\n};\r\n \r\n<span style=\"color: #0000ff;\">auto<\/span> n= 0;\r\n \r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">using<\/span> <span style=\"color: #0000ff;\">namespace<\/span> std::string_literals;\r\n  \r\n  <span style=\"color: #0000ff;\">auto<\/span> n= 0;\r\n  <span style=\"color: #0000ff;\">auto<\/span> s=<span style=\"color: #a31515;\">\"\"<\/span>s;      \r\n  <span style=\"color: #0000ff;\">auto<\/span> t1= T1();               \r\n  <span style=\"color: #0000ff;\">auto<\/span> t2= T2();\r\n    \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"::n \"<\/span> &lt;&lt; ::n &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"n: \"<\/span> &lt;&lt; n &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"s: \"<\/span> &lt;&lt; s &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"T2().mem: \"<\/span> &lt;&lt; T2().mem &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n                      \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Two lines in the source code are especially interesting. First, line 9. The current standard forbids it to initialize non-constant members of a class with auto. Therefore, I have to use the explicit type. This is, from my perspective, contra-intuitive. Here is a discussion of the C++ standardization committee about this issue: article <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2014\/n3897.html\">3897.pdf.<\/a> Second, line 23. C++14 gets C++ string literals. You build them by using a C string literal (<span style=\"font-family: courier new,courier;\">&#8220;&#8221;<\/span>) and adding the suffix s (<span style=\"font-family: courier new,courier;\">&#8220;&#8221;s<\/span>). For convenience, I imported the in line 20: <span style=\"font-family: courier new,courier;\">using namespace std::string_literals<\/span>.&nbsp;<\/p>\n<p>The output of the program is not so thrilling, only for completeness. <span style=\"font-family: courier new,courier;\">T2().mem<\/span> has the value 0.&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4970\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/initAuto.png\" alt=\"initAuto\" width=\"399\" height=\"230\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/initAuto.png 399w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/initAuto-300x173.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/p>\n<h2>Refactorization<\/h2>\n<p>Just now, I want to conclude the post a new use case of <span style=\"font-family: courier new,courier;\">auto<\/span> comes to my mind. <span style=\"font-family: courier new,courier;\">auto<\/span> very well supports the refactorization of your code. First, it&#8217;s very easy to restructure your code if there is no type of information. Second, the compiler automatically takes care of the right types. What does that mean? I answer in the form of a code snippet. At first, the code was without <span style=\"font-family: courier new,courier;\">auto<\/span>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #2b91af;\">int<\/span> a= 5;\r\n<span style=\"color: #2b91af;\">int<\/span> b= 10;\r\n<span style=\"color: #2b91af;\">int<\/span> sum=  a * b * 3;\r\n<span style=\"color: #2b91af;\">int<\/span> res= sum + 10; \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When I replace the variable b of type in with a double 10.5, I have to adjust all dependent types. That is laborious and dangerous. I have to use the right types and take care of narrowing and other <em>intelligent phenomenons<\/em> in C++.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #2b91af;\">int<\/span> a2= 5;\r\n<span style=\"color: #2b91af;\">double<\/span> b2= 10.5;\r\n<span style=\"color: #2b91af;\">double<\/span> sum2= a2 * b2 * 3;\r\n<span style=\"color: #2b91af;\">double<\/span> res2= sum2 * 10.5;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This danger is not present in the case of <span style=\"font-family: courier new,courier;\">auto.<\/span> Everything happens <span style=\"font-family: courier new,courier;\">auto<\/span>-matically.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ refactAuto.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;typeinfo&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #0000ff;\">auto<\/span> a= 5;\r\n  <span style=\"color: #0000ff;\">auto<\/span> b= 10;\r\n  <span style=\"color: #0000ff;\">auto<\/span> sum=  a * b * 3;\r\n  <span style=\"color: #0000ff;\">auto<\/span> res= sum + 10; \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"typeid(res).name(): \"<\/span> &lt;&lt; <span style=\"color: #0000ff;\">typeid<\/span>(res).name() &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">auto<\/span> a2= 5;\r\n  <span style=\"color: #0000ff;\">auto<\/span> b2= 10.5;\r\n  <span style=\"color: #0000ff;\">auto<\/span> sum2= a2 * b2 * 3;\r\n  <span style=\"color: #0000ff;\">auto<\/span> res2= sum2 * 10;  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"typeid(res2).name(): \"<\/span> &lt;&lt; <span style=\"color: #0000ff;\">typeid<\/span>(res2).name() &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">auto<\/span> a3= 5;\r\n  <span style=\"color: #0000ff;\">auto<\/span> b3= 10;\r\n  <span style=\"color: #0000ff;\">auto<\/span> sum3= a3 * b3 * 3.1f;\r\n  <span style=\"color: #0000ff;\">auto<\/span> res3= sum3 * 10;  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"typeid(res3).name(): \"<\/span> &lt;&lt; <span style=\"color: #0000ff;\">typeid<\/span>(res3).name() &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; std::endl;\r\n   \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The slight variations of the code snippet always determine the right type of res, res2, or res3. That&#8217;s the job of the compiler. The variable b2 in line 17 is of type <span style=\"font-family: courier new,courier;\">double<\/span> and therefore also <span style=\"font-family: courier new,courier;\">res2;<\/span> the variable <span style=\"font-family: courier new,courier;\">sum3<\/span> in line 24 becomes due to the multiplication with the <span style=\"font-family: courier new,courier;\">float<\/span> literal 3.1f a float type and the final result <span style=\"font-family: courier new,courier;\">res3.<\/span> To get the type from the compiler, I use the <span style=\"font-family: courier new,courier;\">typeid<\/span> operator defined in the header <span style=\"font-family: courier new,courier;\">typeinfo.<\/span><\/p>\n<p>Here you get the results in black on yellow.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4971\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/autoRefact.png\" alt=\"autoRefact\" width=\"543\" height=\"205\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/autoRefact.png 543w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/10\/autoRefact-300x113.png 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/p>\n<p>Impressed? Me too.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The initialization with curly braces {} has much in common with auto. It is used similarly often, helps to read the code, and makes your code safer. How? You will see it in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/initialization\">next post.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Probably the most frequently used feature of C++11 is auto. Thanks to auto, the compiler determines the type of a variable from its initializer. But what is the point of safety-critical software?&nbsp;<\/p>\n","protected":false},"author":21,"featured_media":4968,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[460],"class_list":["post-4972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-initialization"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4972","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=4972"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4972\/revisions"}],"predecessor-version":[{"id":6939,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4972\/revisions\/6939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4968"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}