{"id":1679,"date":"2018-01-24T21:02:37","date_gmt":"2018-01-24T20:02:37","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=1679"},"modified":"2018-01-24T21:02:37","modified_gmt":"2018-01-24T20:02:37","slug":"c17-initializers-for-if-switch-statements","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2018\/01\/24\/c17-initializers-for-if-switch-statements\/","title":{"rendered":"C++17: Initializers for if &#038; switch statements"},"content":{"rendered":"<p>Two small, but very useful C++17 features are initializers for <strong>if<\/strong> and <strong>switch<\/strong> statements. These can be used to prevent polluting the enclosing scope with variables that should only be scoped to the <strong>if<\/strong> and <strong>switch<\/strong> statement. The <strong>for<\/strong> statement already supports such initializers since the beginning.<br \/>\nFor example, suppose you are using a map to store the number of times a certain name occurs:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;cstddef&gt;\r\n#include &lt;map&gt;\r\n#include &lt;string&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n    std::map&lt;std::string, size_t&gt; nameCounters{\r\n        {&quot;Marc&quot;, 5},\r\n        {&quot;Bob&quot;, 12},\r\n        {&quot;John&quot;, 3}\r\n    };\r\n\r\n    auto result = nameCounters.find(&quot;Bob&quot;);\r\n    if (result != cend(nameCounters))\r\n        std::cout &lt;&lt; &quot;Count: &quot; &lt;&lt; result-&gt;second &lt;&lt; std::endl;\r\n}<\/pre>\n<p>In this example I&#8217;m searching for a key in the map, and if the key is found, I output the associated value to the console. The result of calling find() is stored in result, but this variable is only used inside the <strong>if<\/strong> statement.<br \/>\nWith C++17, you can initialize the result variable with an <strong>if statement initializer<\/strong> so that the result variable is only known inside the <strong>if<\/strong> statement and does not pollute the enclosing scope with unnecessary variables:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;cstddef&gt;\r\n#include &lt;map&gt;\r\n#include &lt;string&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n    \/\/ ... Initialize map as before ...\r\n\r\n    if (auto result = nameCounters.find(&quot;Bob&quot;); result != cend(nameCounters))\r\n        std::cout &lt;&lt; &quot;Count: &quot; &lt;&lt; result-&gt;second &lt;&lt; std::endl;\r\n}<\/pre>\n<p>Similar kind of initializers are supported for <strong>switch<\/strong> statements:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">switch (&lt;initializer&gt;; &lt;expression&gt;) { \/* ... *\/ }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Two small, but very useful C++17 features are initializers for if and switch statements. These can be used to prevent polluting the enclosing scope with variables that should only be scoped to the if and switch statement. The for statement already supports such initializers since the beginning. For example, suppose you are using a map [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[222,228],"class_list":["post-1679","post","type-post","status-publish","format-standard","hentry","category-c","tag-c17","tag-visual-c-2017"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1679"}],"version-history":[{"count":16,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1679\/revisions"}],"predecessor-version":[{"id":1695,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1679\/revisions\/1695"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}