{"id":5245,"date":"2017-07-25T19:43:22","date_gmt":"2017-07-25T19:43:22","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5245"},"modified":"2017-07-25T19:43:22","modified_gmt":"2017-07-25T19:43:22","slug":"folding-expressions-in-c-17","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/folding-expressions-in-c-17\/","title":{"rendered":"Folding expressions in C++ 17"},"content":{"rendered":"<p><strong>Setup your dev tools to use C++ 17<\/strong><\/p>\n<p>Using CLion (or CMake) and setting the CMakeLists.txt CMAKE_CXX_STANDARD to <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nset(CMAKE_CXX_STANDARD 17)\r\n<\/pre>\n<p>allows us to use C++ 17 features.<\/p>\n<p><em>In CLion the intellisense\/editor will show folding expressions with red underlines, i.e. as errors in the current release, however the code will compile<\/em><\/p>\n<p><strong>Folding Expressions<\/strong><\/p>\n<p>Basically folding expressions allow us to write code which takes one or more arguments and applies operators to them to create a return value.<\/p>\n<p>In C++ terms, <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/fold\" target=\"_blank\">folding expressions<\/a> take variadic template arguments and unpack the arguments using unary or binary operators to create a return value. <\/p>\n<p>The syntax for folding expressions, is as follows<\/p>\n<p>Syntax (as taken from http:\/\/en.cppreference.com\/w\/cpp\/language\/fold).<\/p>\n<ul>\n<li><em>unary right fold<\/em> (pack op &#8230;)<\/li>\n<li><em>unary left fold<\/em> (&#8230; op pack)<\/li>\n<li><em>binary right fold<\/em> (pack op &#8230; op init)<\/li>\n<li><em>binary left fold<\/em> (init op &#8230; op pack)<\/li>\n<\/ul>\n<p>Let&#8217;s look at some examples to make things clearer using this syntax.<\/p>\n<p><em>Unary right fold<\/em><\/p>\n<p>The following is a simple summation method using unary right fold syntax<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntemplate&lt;typename... Args&gt;\r\nauto unary_right_fold(Args... args) {\r\n    return (args + ...);\r\n}\r\n<\/pre>\n<p><em>Unary left fold<\/em><\/p>\n<p>The following is a simple summation method using unary left fold syntax<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntemplate&lt;typename... Args&gt;\r\nauto unary_left_fold(Args... args) {\r\n    return (... + args);\r\n}\r\n<\/pre>\n<p><em>Binary right fold<\/em><\/p>\n<p>The following is a simple summation + 1 method using binary left fold syntax<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntemplate&lt;typename... Args&gt;\r\nauto binary_right_fold(Args... args) {\r\n    return (args + ... + 1);\r\n}\r\n<\/pre>\n<p><em>Binary left fold<\/em><\/p>\n<p>The following is a simple 1 + summation method using binary left fold syntax<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntemplate&lt;typename... Args&gt;\r\nauto binary_left_fold(Args... args) {\r\n    return (1 + ... + args);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Setup your dev tools to use C++ 17 Using CLion (or CMake) and setting the CMakeLists.txt CMAKE_CXX_STANDARD to set(CMAKE_CXX_STANDARD 17) allows us to use C++ 17 features. In CLion the intellisense\/editor will show folding expressions with red underlines, i.e. as errors in the current release, however the code will compile Folding Expressions Basically folding expressions [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[180],"tags":[],"class_list":["post-5245","post","type-post","status-publish","format-standard","hentry","category-cc"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5245"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5245\/revisions"}],"predecessor-version":[{"id":5258,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5245\/revisions\/5258"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}