{"id":321,"date":"2018-09-11T09:57:05","date_gmt":"2018-09-11T07:57:05","guid":{"rendered":"https:\/\/www.scionova.com\/?p=321"},"modified":"2019-01-10T11:23:10","modified_gmt":"2019-01-10T10:23:10","slug":"6-useful-new-features-in-c17-part-4","status":"publish","type":"post","link":"https:\/\/www.scionova.com\/en\/6-useful-new-features-in-c17-part-4\/","title":{"rendered":"6 Useful New Features in C++17 &#8211; Part 4"},"content":{"rendered":"<h4><em>This is the fourth 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>If Statements with constexpr Condition<\/h3>\n<p>This is a feature that perhaps has its main use while working with templated classes and such, but there are other uses as well, e.g., together with variadic arguments. constexpr adds the ability to discard branches at compile time. You might argue that a compiler optimizes code to achieve the same behavior anyway, and of course, you are correct. However, using constexpr if guarantees that it is evaluated at compile time, and at the very least the code compiles faster.<\/p>\n<p>I would like to show an example when one makes use of SFINAE, a situation where I think constexpr if is useful. What follows is a non-sensical code snippet using SFINAE and C++14 revision.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-325\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/const.cond_.1-300x111.png\" alt=\"\" width=\"781\" height=\"289\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/const.cond_.1-300x111.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/const.cond_.1-768x285.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/const.cond_.1.png 943w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/p>\n<p>With the use of constexpr if in C++17 it can be written with only one function, like so:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-372\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/09\/constexprif2-300x104.png\" alt=\"\" width=\"776\" height=\"269\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/09\/constexprif2-300x104.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/09\/constexprif2-768x265.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/09\/constexprif2-1024x354.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/09\/constexprif2.png 1360w\" sizes=\"(max-width: 776px) 100vw, 776px\" \/><\/p>\n<p>I know which version I would like to work with.<\/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-304\" src=\"https:\/\/scionova.evdy.se\/wp-content\/uploads\/2018\/08\/part4-300x69.png\" alt=\"\" width=\"761\" height=\"175\" srcset=\"https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part4-300x69.png 300w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part4-768x177.png 768w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part4-1024x236.png 1024w, https:\/\/www.scionova.com\/wp-content\/uploads\/2018\/08\/part4.png 1248w\" sizes=\"(max-width: 761px) 100vw, 761px\" \/><\/p>\n<p>&nbsp;<\/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 5 &#8220;6 Useful New Features in C++17&#8221; <a href=\"https:\/\/scionova.evdy.se\/2018\/09\/13\/6-useful-new-features-in-c17-part-5\/\">HERE<\/a>.<\/h4>\n<p>&nbsp;<\/p>\n<p>\/\/ Patrik Ingmarsson<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the fourth part of the series &#8220;6 Useful New Features in C++17&#8221;, the first part can be found here. If Statements with constexpr Condition This is a feature that perhaps has its main use while working with templated classes and such, but there are other uses as well, e.g., together with variadic arguments. [&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\/321"}],"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=321"}],"version-history":[{"count":0,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/posts\/321\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/media?parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/categories?post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scionova.com\/en\/wp-json\/wp\/v2\/tags?post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}