{"id":75,"date":"2018-12-24T05:13:05","date_gmt":"2018-12-24T08:13:05","guid":{"rendered":"http:\/\/35.243.195.209\/?p=75"},"modified":"2018-12-24T05:13:05","modified_gmt":"2018-12-24T08:13:05","slug":"auto-in-c","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2018\/12\/24\/auto-in-c\/","title":{"rendered":"Auto in C++"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce <code>auto<\/code> and <code>decltype<\/code> in C++ 11.<\/p>\n<h2>Why Auto<\/h2>\n<p>In some situations, <code>auto<\/code> makes codes more readable. It will tell the compiler to deduce the type of a declared variable from the initialization expression. The <a href=\"&quot;https:\/\/en.cppreference.com\/w\/cpp\/language\/auto&quot;\">details<\/a> are complex. At present, we want to make codes correct, clean, and simple.<\/p>\n<p>As it is said, <strong>keep things consistent<\/strong> in your codes.<\/p>\n<h2>Good Situations<\/h2>\n<p>The following situations are good to use <code>auto<\/code>.<\/p>\n<h3>Before <code>make_xxx<\/code><\/h3>\n<p>It is very clear for reviewers that what type the variable should have when calling <code>make_xxx<\/code>.<\/p>\n<pre><code class=\"c\">\/\/without auto. Not that good, looks cumbersome\nSomeType&lt;OtherType&gt;::SomeOtherType * obj1 = new SomeType&lt;OtherType&gt;::SomeOtherType();\nstd::shared_ptr&lt;XyzType&gt; obj2 = std::make_shared&lt;XyzType&gt;(args...);\nstd::unique_ptr&lt;XyzType&gt; obj2 = std::make_unique&lt;XyzType&gt;(args...);\n\n\/\/With auto. good : auto increases readability here\nauto obj1 = new SomeType&lt;OtherType&gt;::SomeOtherType();\nauto obj2 = std::make_shared&lt;XyzType&gt;(args...);\nauto obj3 = std::make_unique&lt;XyzType&gt;(args...);\n<\/code><\/pre>\n<h3>Iterator-based Loop or For-Each Loop<\/h3>\n<p>It is a good idea to use <code>auto<\/code> in iterator-based loop or for-each loop.<\/p>\n<pre><code class=\"c\">\/\/without auto.\nfor (std::map&lt;std::wstring, std::map&lt;std::wstring, int&gt;&gt;::iterator outerMap_Iter = StudentGrades.begin(); outerMap_Iter != StudentGrades.end(); ++outerMap_Iter)\n{\n    \/\/Print out the student name\n    std::wcout &lt;&lt; outerMap_Iter-&gt;first &lt;&lt; std::endl;\n    for (std::map&lt;std::wstring, int&gt;::iterator innerMap_Iter = outerMap_Iter-&gt;second.begin(); innerMap_Iter != outerMap_Iter-&gt;second.end(); ++innerMap_Iter)\n    {\n        \/\/Print the grades here\n        std::wcout &lt;&lt; innerMap_Iter-&gt;first &lt;&lt; \" : \" &lt;&lt; innerMap_Iter-&gt;second &lt;&lt; std::endl;\n    }\n    std::wcout &lt;&lt; std::endl;\n}\n\n\/\/With auto.\nfor (auto outerMap_Iter = StudentGrades.begin(); outerMap_Iter != StudentGrades.end(); ++outerMap_Iter) \n{\n  \/\/Print out the student name\n  std::wcout &lt;&lt; outerMap_Iter-&gt;first &lt;&lt; std::endl;\n\n  for (auto innerMap_Iter = outerMap_Iter-&gt;second.begin(); innerMap_Iter != outerMap_Iter-&gt;second.end(); ++innerMap_Iter)\n  {\n     \/\/Print the grades here\n     std::wcout &lt;&lt; innerMap_Iter-&gt;first &lt;&lt; \" : \" &lt;&lt; innerMap_Iter-&gt;second &lt;&lt; std::endl;\n  }\n  std::wcout &lt;&lt; std::endl;\n}\n\n\/\/ Best\nfor (auto const &amp;outer_iter : StudentGrades) \n{\n  std::wcout &lt;&lt; outer_iter.first &lt;&lt; std::endl;\n\n  for (auto const &amp;inner_iter : outer_iter.second)\n  {\n     std::wcout &lt;&lt; inner_iter.first &lt;&lt; \" : \" &lt;&lt; inner_iter.second &lt;&lt; std::endl;\n  }\n}\n<\/code><\/pre>\n<p>I will introduce the difference of <code>auto const<\/code>, <code>auto &amp;<\/code>, etc in future posts.<\/p>\n<h3>Lambda Functions<\/h3>\n<p>Lambda functions are good when you need anonymous functions. I will introduce Lambda function in future posts.<\/p>\n<pre><code class=\"c\">\/\/ Use function object\nstd::function&lt;int(int, int)&gt; func_multiply2 = [](int a, int b) -&gt; int { return a * b; };\nstd::cout &lt;&lt; func_multiply2(2, 3) &lt;&lt; std::endl;\n\n\/\/ Use auto\nauto func_multiply = [](int a, int b) -&gt; int { return a * b; };\n<\/code><\/pre>\n<h2>Bad Situations<\/h2>\n<h3>Intercept the value of functions<\/h3>\n<p>It is very confusing to get a value without readable type which means you may need to go to another file to search the definitions.<\/p>\n<pre><code>\/\/ confusing\nauto a = ConjureMagic();\nSetMagic(a);\n<\/code><\/pre>\n<h2>Method Arguments<\/h2>\n<p>It will take considerable codes to catch all the exceptions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce auto and decltype in C++ 11. Why Auto In some situations, auto makes codes more readable. It will tell the compiler to deduce the type of a declared variable from the initialization expression. The details are complex. At present, we want to make codes correct, clean, and simple&#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-75","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\/75","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=75"}],"version-history":[{"count":3,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":78,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/75\/revisions\/78"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}