{"id":299,"date":"2018-09-06T16:17:46","date_gmt":"2018-09-06T14:17:46","guid":{"rendered":"https:\/\/www.scionova.com\/?p=299"},"modified":"2019-01-10T11:21:42","modified_gmt":"2019-01-10T10:21:42","slug":"6-useful-new-features-in-c17-part-3","status":"publish","type":"post","link":"https:\/\/www.scionova.com\/en\/6-useful-new-features-in-c17-part-3\/","title":{"rendered":"6 Useful New Features in C++17 &#8211; Part 3"},"content":{"rendered":"<h4><em>This is the third part of the series &#8220;6 Useful New Features in C++17&#8221;, the first part can be found <a href=\"https:\/\/scionova.evdy.se\/2018\/08\/29\/6-useful-new-features-in-c17-part-1\/\">here<\/a>.<\/em><\/h4>\n<h3>Init statements in if statements<\/h3>\n<p>Like for statements, <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2016\/p0305r1.html\">if and switch statements get an initializer part<\/a>. Convenient when handling shared resources between threads. As an example, if there are a lot of workers that share a single job queue but the jobs themselves are independent, then we only want to block while popping the queue and not while executing the job. One could solve this with the following:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-312\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/initif1-300x137.png\" alt=\"\" width=\"839\" height=\"384\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/initif1-300x137.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/initif1-768x351.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/initif1-1024x468.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/initif1.png 1360w\" sizes=\"(max-width: 839px) 100vw, 839px\" \/><\/p>\n<p>The extra scope makes sure that we release the guard after we successfully grabbed a job to process. In my opinion it looks a bit messy and we&#8217;ve added extra parentheses to keep track of. If we instead make use of the init statement it could be rewritten as follows:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-313\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/initif2-300x103.png\" alt=\"\" width=\"836\" height=\"287\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/initif2-300x103.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/initif2-768x263.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/initif2-1024x351.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/initif2.png 1482w\" sizes=\"(max-width: 836px) 100vw, 836px\" \/><\/p>\n<p>At least I think this is easier to follow and less error prone while refactoring.<\/p>\n<h3><strong>Compiler Support<\/strong><\/h3>\n<p>These features are all fully supported by GCC, MSVC, and clang. Below follows a table showing which version enables support for each feature respectively.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-303\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/part3-300x58.png\" alt=\"\" width=\"765\" height=\"148\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part3-300x58.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part3-768x149.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part3-1024x199.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part3.png 1248w\" sizes=\"(max-width: 765px) 100vw, 765px\" \/><\/p>\n<p>For more a detailed matrix with more compilers and all features of C++17 listed, follow this <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/compiler_support\">link<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h4>READ PART 4 &#8220;6 Useful New Features in C++17&#8221; <a href=\"https:\/\/scionova.evdy.se\/2018\/09\/11\/6-useful-new-features-in-c17-part-4\/\">HERE<\/a>.<\/h4>\n<p>&nbsp;<\/p>\n<p>\/\/ Patrik Ingmarsson<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the third part of the series &#8220;6 Useful New Features in C++17&#8221;, the first part can be found here. Init statements in if statements Like for statements, if and switch statements get an initializer part. Convenient when handling shared resources between threads. As an example, if there are a lot of workers that [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/posts\/299"}],"collection":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":0,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}