{"id":6221,"date":"2021-09-16T06:10:33","date_gmt":"2021-09-16T06:10:33","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/smart-tricks-with-fold-expressions\/"},"modified":"2024-07-22T10:47:30","modified_gmt":"2024-07-22T10:47:30","slug":"smart-tricks-with-fold-expressions","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/smart-tricks-with-fold-expressions\/","title":{"rendered":"Smart Tricks with Parameter Packs and Fold Expressions"},"content":{"rendered":"<p>To complete my post about variadic templates and fold expressions, I present in this post smart tricks using parameter packs and fold expressions.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6208\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/templates.png\" alt=\"FoldExpressions\" width=\"650\" height=\"399\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/templates.png 912w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/templates-300x184.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/templates-768x472.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Fold expressions enable it to reduce a parameter pack with a binary operator. Thanks to them, you can write concise expressions for repeated operations. This repeated operation can be a print function or a push_back function to push elements onto a vector. Let me start with the print function.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ printFoldExpressions.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> ... Args<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> printMe(Args<span style=\"color: #555555;\">&amp;&amp;<\/span> ... args) {\n    (std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span>  ... <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>forward<span style=\"color: #555555;\">&lt;<\/span>Args<span style=\"color: #555555;\">&gt;<\/span>(args)) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;\n\n    printMe();\n    printMe(<span style=\"color: #cc3300;\">\"Rainer \"<\/span>, <span style=\"color: #cc3300;\">\"Grimm\"<\/span>);\n    printMe(<span style=\"color: #336666;\">true<\/span>, <span style=\"color: #cc3300;\">\" \"<\/span>, <span style=\"color: #cc3300;\">\"+\"<\/span>, <span style=\"color: #cc3300;\">\" \"<\/span>,<span style=\"color: #336666;\">false<\/span>, <span style=\"color: #cc3300;\">\" = \"<\/span>, <span style=\"color: #336666;\">true<\/span> <span style=\"color: #555555;\">+<\/span> <span style=\"color: #336666;\">false<\/span>);\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}\n<\/pre>\n<\/div>\n<p>The <code>printMe <\/code>function can accept an arbitrary number of arguments. In the concrete function, this means no argument, two C-strings, and a few strings and numbers. The <code>printMe<\/code> function automatically deduces their types and displays them. Three powerful C++ techniques are involved.<\/p>\n<ul>\n<li>Variadic templates (<code> ...<\/code> ): accepts an arbitrary number of arguments. Read more here: &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/variadic-templates-or-the-power-of-three-dots\">Variadic Templates or the Power of Three Dots<\/a>&#8221; and &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/more-arbout-variadic-templates\">More about Variadic Templates<\/a>&#8220;.<\/li>\n<li>Perfect forwarding (<code>std::forward<\/code>): forwards the arguments without changing their value category. Read more here: <a href=\"https:\/\/www.modernescpp.com\/index.php\/perfect-forwarding\">Perfect Forwarding<\/a>.<\/li>\n<li>Fold expressions <code>(std::cout &lt;&lt; ... &lt;&lt; std::forward&lt;Args&gt;(args)<\/code>): reduces the parameter pack from the left using the binary operator<code> &lt;&lt;<\/code> and the initial value <code>std::cout<\/code>. Read more here: <a href=\"https:\/\/www.modernescpp.com\/index.php\/from-variadic-templates-to-fold-expressions\">From Variadic Templates to Fold Expressions<\/a>.\u00a0<code><br \/>\n<\/code><\/li>\n<\/ul>\n<p>Finally, here is the output of the program. <img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6218\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/printFoldExpressions.png\" alt=\"printFoldExpressions\" width=\"425\" height=\"255\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/printFoldExpressions.png 425w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/printFoldExpressions-300x180.png 300w\" sizes=\"auto, (max-width: 425px) 100vw, 425px\" \/><\/p>\n<p>Thanks to fold expressions, you can push an arbitrary number of arguments onto a vector.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ pushBackFoldExpressions.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std;\n \n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span>... Args<span style=\"color: #555555;\">&gt;<\/span>\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> myPushBack(vector<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&amp;<\/span> v, Args<span style=\"color: #555555;\">&amp;&amp;<\/span>... args) {\n    (v.push_back(args), ...);                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n}\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myIntVec;\n\tmyPushBack(myIntVec, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">5<\/span>, <span style=\"color: #ff6600;\">6<\/span>, <span style=\"color: #ff6600;\">7<\/span>, <span style=\"color: #ff6600;\">8<\/span>, <span style=\"color: #ff6600;\">9<\/span>, <span style=\"color: #ff6600;\">10<\/span>);\n\t<span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> v <span style=\"color: #555555;\">:<\/span> myIntVec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">' '<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>vector myDoubleVec{<span style=\"color: #ff6600;\">1.1<\/span>, <span style=\"color: #ff6600;\">2.2<\/span>, <span style=\"color: #ff6600;\">3.3<\/span>};      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n    myPushBack(myDoubleVec, <span style=\"color: #ff6600;\">4.4<\/span>, <span style=\"color: #ff6600;\">5.5<\/span>, <span style=\"color: #ff6600;\">6.6<\/span>);\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> v<span style=\"color: #555555;\">:<\/span> myDoubleVec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">' '<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"\\n\\n\"<\/span>;\n\n}\n<\/pre>\n<\/div>\n<p>Lines (1) and (2) are the most interesting ones. (2) pushes the three doubles onto the vector. With C++17, the compiler can automatically deduce the types of arguments. The expression<code> (v.push_back(args),...)<\/code> pushes the elements from the right using the binary comma operator (<code>,<\/code>). Alternatively, I could also push from the left <code>(..., v.push_back(args))<\/code>, because the comma operator is associative. Honestly, this looks weird. Therefore, I prefer the first variant.<\/p>\n<p>The following screenshot shows the program output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6219\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/pushBackFoldExpressions.png\" alt=\"pushBackFoldExpressions\" width=\"513\" height=\"250\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/pushBackFoldExpressions.png 513w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/pushBackFoldExpressions-300x146.png 300w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><\/p>\n<p>Now, I want to go one stack back from fold expressions to variadic templates and present the overload pattern. The overload pattern is a clever way to wrap multiple lambdas into an overload set.<\/p>\n<p>Johnathan O&#8217;Connor called my attention to the fact that the article<a href=\"https:\/\/www.foonathan.net\/2020\/05\/fold-tricks\/#Content\"> Nifty Fold Expressions Tricks<\/a> by Jonathan M\u00fcller provides more fold tricks.<\/p>\n<h2>Overload Pattern<\/h2>\n<p>I want to make it short. Here is the overload pattern implemented with C++20:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename <\/span>... Ts<span style=\"color: #555555;\">&gt;<\/span> <span style=\"color: #006699; font-weight: bold;\">struct<\/span> Overload <span style=\"color: #555555;\">:<\/span> Ts ... { <span style=\"color: #006699; font-weight: bold;\">using<\/span> Ts<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">operator<\/span>() ... ; };\n<\/pre>\n<\/div>\n<p>What? Sorry, my mistake. I should lay it out properly.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename <\/span>... Ts<span style=\"color: #555555;\">&gt;<\/span> \n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Overload <span style=\"color: #555555;\">:<\/span> Ts ... { \n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> Ts<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">operator<\/span>() ... ; \n};\n<\/pre>\n<\/div>\n<p>The struct <code>Overload<\/code> can have arbitrarily many base classes (<code>Ts ...<\/code>). It derives from each class <code>public<\/code> and brings the call operator (<code>Ts::operator..<\/code>.) of each base class into its scope.<\/p>\n<p>There is more to explain about these four magic lines of code. Before I do that in my next post, let me use the overload pattern to display the types of integral literals. The following program requires a C++20 compiler.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ overloadPattern.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> ... Ts<span style=\"color: #555555;\">&gt;<\/span> \n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Overload <span style=\"color: #555555;\">:<\/span> Ts ... { \n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> Ts<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">operator<\/span>() ...;\n};\n\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> TypeOfIntegral <span style=\"color: #555555;\">=<\/span> Overload {\n        [](<span style=\"color: #007788; font-weight: bold;\">int<\/span>) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"  int\"<\/span>; },\n        [](<span style=\"color: #007788; font-weight: bold;\">unsigned<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\" unsigned int\"<\/span>; },\n        [](<span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\" long int\"<\/span>; },\n        [](<span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"long long int\"<\/span>; },\n        [](<span style=\"color: #006699; font-weight: bold;\">auto<\/span>) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"unknown type\"<\/span>; },\n    };\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5u): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5u<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5U): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5U<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5l): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5l<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5L): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5L<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5ll): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5ll<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5LL): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5LL<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5ul): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5ul<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TypeOfIntegral(5.5): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> TypeOfIntegral(<span style=\"color: #ff6600;\">5.5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>; \n\n}\n<\/pre>\n<\/div>\n<p>In the program <code>overloadPattern.cpp<\/code>, the overload set consists of lambda expressions accepting an <code>int<\/code>, an<code> unsigned int<\/code>, a <code>long int<\/code>, a<code> long long int<\/code>, and<code> auto<\/code>. <code>auto<\/code> is the fallback used, for example, if the overload set is invoked with an unknown type. This happens when I invoke <code>TypeOfIntegral<\/code> with an <code>unsigned long<\/code> or a <code>double<\/code> value.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6220\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/overloadPattern.png\" alt=\"overloadPattern\" width=\"403\" height=\"388\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/overloadPattern.png 403w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/overloadPattern-300x289.png 300w\" sizes=\"auto, (max-width: 403px) 100vw, 403px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Typically, you use the overload pattern for a<code> std::variant<\/code>. <code>std::variant<\/code> is a type-safe union. An instance <code>var<\/code> of<code> std::variant<\/code> (C++17) has one value from one of its types.<code> std::visit<\/code> allows you to apply a visitor to<code> var<\/code>. Exactly, here comes the overload pattern convenient into play. Read more about <code>std::variant, std::visit<\/code>, and the overload pattern in my next post.<\/p>\n<h2>Pdf Bundle: C++20 Modules<\/h2>\n<p>Based on the last poll, I&#8217;ve created the next pdf bundle.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5293\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/08\/bundle.png\" alt=\"bundle\" width=\"500\" height=\"488\" \/><\/p>\n<p>The pdf bundle includes all<span class=\"css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0\"><br \/>\n<\/span><\/p>\n<ul>\n<li><span class=\"css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0\"> posts. <\/span><\/li>\n<li><span class=\"css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0\">source code files to these posts.<\/span><\/li>\n<\/ul>\n<p><span class=\"css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0\">Here is more info on how to get the pdf bundle: <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-new-pdf-bundle-is-ready-c-20-modules\">The New pdf Bundle is Ready: C++20 Modules<\/a><br \/>\n<\/span><\/p>\n<p><span class=\"css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To complete my post about variadic templates and fold expressions, I present in this post smart tricks using parameter packs and fold expressions.<\/p>\n","protected":false},"author":21,"featured_media":6208,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[442,441,440],"class_list":["post-6221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates","tag-fold-expressions","tag-variadic-templates","tag-variant"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6221"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6221\/revisions"}],"predecessor-version":[{"id":9759,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6221\/revisions\/9759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6208"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}