{"id":288,"date":"2018-09-03T16:01:17","date_gmt":"2018-09-03T14:01:17","guid":{"rendered":"https:\/\/www.scionova.com\/?p=288"},"modified":"2019-01-10T11:20:28","modified_gmt":"2019-01-10T10:20:28","slug":"6-useful-new-features-in-c17-part-2","status":"publish","type":"post","link":"https:\/\/www.scionova.com\/en\/6-useful-new-features-in-c17-part-2\/","title":{"rendered":"6 Useful New Features in C++17 &#8211; Part 2"},"content":{"rendered":"<h4><em>This is the second 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<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-364\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/Bild-kod-300x148.jpg\" alt=\"\" width=\"596\" height=\"295\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/Bild-kod-300x148.jpg 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/Bild-kod-768x379.jpg 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/Bild-kod-1024x506.jpg 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/Bild-kod.jpg 1440w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Structured Binding<\/strong><\/h3>\n<p>One thing that has been bugging me when trying to write nice looking code is iterating through maps. Most often it is when acting on information read from a JSON or XML file, where if a key is found then something should be done with the value.<\/p>\n<p>For example, an application has a set of settings with default values, but a user can set their own values for these settings. However, the file specifying these user specific values only needs to contain those settings that deviate from the default values. A code snippet doing that could look like:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-289\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/structbind1-300x79.png\" alt=\"\" width=\"915\" height=\"241\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind1-300x79.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind1-768x201.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind1.png 1360w\" sizes=\"(max-width: 915px) 100vw, 915px\" \/><\/p>\n<p>With <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/structured_binding\">Structured Binding<\/a> supported by C++17 this can be rewritten in a prettier and more readable way:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-290\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/structbind2-300x79.png\" alt=\"\" width=\"919\" height=\"242\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind2-300x79.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind2-768x201.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind2-1024x268.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind2.png 1360w\" sizes=\"(max-width: 919px) 100vw, 919px\" \/><\/p>\n<p>One other example where structured binding helps you clean up your code would be when one needs to handle tuples.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-291\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/structbind3-300x62.png\" alt=\"\" width=\"890\" height=\"183\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind3-300x62.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind3-768x158.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind3-1024x211.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind3.png 1360w\" sizes=\"(max-width: 890px) 100vw, 890px\" \/><\/p>\n<p>Doesn&#8217;t look very intuitive even in this very simple example. What we could do before C++17 is make use of std::tiewhich unpacks the tuple into independent variables.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-292\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/structbind4-300x87.png\" alt=\"\" width=\"917\" height=\"266\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind4-300x87.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind4-768x222.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind4-1024x297.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind4.png 1360w\" sizes=\"(max-width: 917px) 100vw, 917px\" \/><\/p>\n<p>Well, I like this better. However, it is a bit cluttered because we need to declare the variables which is used in std::tie. Fortunately, structured binding lets us get rid of that as well:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-293\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/structbind5-300x53.png\" alt=\"\" width=\"911\" height=\"161\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind5-300x53.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind5-768x137.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind5-1024x182.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/structbind5.png 1360w\" sizes=\"(max-width: 911px) 100vw, 911px\" \/><\/p>\n<p>Please note that structured binding can be used with other types than tuples!<\/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-301\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/part2-300x47.png\" alt=\"\" width=\"906\" height=\"142\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part2-300x47.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part2-768x119.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part2-1024x159.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part2.png 1248w\" sizes=\"(max-width: 906px) 100vw, 906px\" \/><\/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<h4><\/h4>\n<h4>READ PART 3 &#8220;6 Useful New Features in C++17&#8221;\u00a0<a href=\"https:\/\/scionova.evdy.se\/2018\/09\/06\/6-useful-new-features-in-c17-part-3\/\">HERE<\/a>.<\/h4>\n<p>\/\/ Patrik Ingmarsson<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second part of the series &#8220;6 Useful New Features in C++17&#8221;, the first part can be found here. &nbsp; Structured Binding One thing that has been bugging me when trying to write nice looking code is iterating through maps. Most often it is when acting on information read from a JSON or [&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\/288"}],"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=288"}],"version-history":[{"count":0,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/posts\/288\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/media?parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/categories?post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/tags?post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}