{"id":8234,"date":"2023-09-18T09:28:28","date_gmt":"2023-09-18T09:28:28","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=8234"},"modified":"2023-09-18T09:28:28","modified_gmt":"2023-09-18T09:28:28","slug":"c23-ranges-improvements-and-stdgenerator","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c23-ranges-improvements-and-stdgenerator\/","title":{"rendered":"C++23: Ranges Improvements and std::generator"},"content":{"rendered":"\n<p>C++20 does not provide concrete coroutines, but C++20 provides a framework for implementing coroutines. This changes with C++23.<code> std::generator<\/code> is the first concrete coroutine.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/Cpp23-3-1030x579.png\" alt=\"\" class=\"wp-image-8236\" style=\"width:745px;height:419px\" width=\"745\" height=\"419\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/Cpp23-3-1030x579.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/Cpp23-3-300x169.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/Cpp23-3-768x432.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/Cpp23-3-705x397.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/Cpp23-3.png 1280w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><\/figure>\n\n\n\n<p><code>std::generator<\/code> is part of the extension of the ranges library in C++23. So, let me start this post with the ranges library in C++20 and its extension in C++23. I will make this short. I already wrote about the ranges library in C++20 and its extension in C++23:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/tag\/ranges\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/tag\/ranges\/\">My ranges posts<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/ranges-improvements-with-c-23\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/ranges-improvements-with-c-23\/\">Ranges Improvements in C++23<\/a><\/li>\n<\/ul>\n\n\n\n<p>There is only one story I want to complete. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python&#8217;s range function in C++23<\/h2>\n\n\n\n<p>In my posts &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-pythonic-with-the-ranges-library\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/c-20-pythonic-with-the-ranges-library\/\">C++20: Pythonic with the Ranges Library<\/a>&#8221; and &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-more-pythonic-with-the-ranges-library\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/c-20-more-pythonic-with-the-ranges-library\/\">C++20: Pythons range Function<\/a>&#8220;, I implemented Pythons&#8217;s 2 <a href=\"https:\/\/cs.stanford.edu\/people\/nick\/py\/python-range.html\" data-type=\"link\" data-id=\"https:\/\/cs.stanford.edu\/people\/nick\/py\/python-range.html\">range <\/a>function with the ranges library.  The difference between Python&#8217;s 2 and Python&#8217;s 3 range function is that the 2 version is eager, but the 3 version is lazy.  This means that the 2 version creates the numbers, but the 3 version returns a generator to create the numbers on request. I could only solve the challenge in C++20 by using the <a href=\"https:\/\/github.com\/ericniebler\/range-v3\">range-v3<\/a> library from Eric Niebler. In particular, I needed the function <code>stride(N)<\/code>,  that advanced over another view and returns the<code> N<\/code> element each time. <\/p>\n\n\n\n<p><a href=\"std::ranges::views::stride\"><code>std::ranges::views::stride<\/code><\/a> is part of C++23, and the GCC and MSVC compilers support it. Additionally, I use in this example the convenience function <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/ranges\/to\">std::ranges::to<\/a><\/code>. This C++23 function enables you to construct containers and strings from ranges. Only the MSVC and Clang compilers support this function. <\/p>\n\n\n\n<p> Finally, here is Python&#8217;s <a href=\"https:\/\/cs.stanford.edu\/people\/nick\/py\/python-range.html\" data-type=\"link\" data-id=\"https:\/\/cs.stanford.edu\/people\/nick\/py\/python-range.html\">range<\/a> function in C++.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto; gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ rangeCpp23.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;ranges&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;vector&gt;<\/span>\n\nstd<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> range(<span style=\"color: #007788; font-weight: bold\">int<\/span> begin, <span style=\"color: #007788; font-weight: bold\">int<\/span> end, <span style=\"color: #007788; font-weight: bold\">int<\/span> stepsize <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">1<\/span>) {\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> result{};\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (begin <span style=\"color: #555555\">&lt;<\/span> end) {                                  <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)                                <\/span>\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> boundary <span style=\"color: #555555\">=<\/span> [end](<span style=\"color: #007788; font-weight: bold\">int<\/span> i){ <span style=\"color: #006699; font-weight: bold\">return<\/span> i <span style=\"color: #555555\">&lt;<\/span> end; };\n        result <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>ranges<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>iota(begin) <span style=\"color: #555555\">|<\/span> std<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>stride(stepsize) \n                                                 <span style=\"color: #555555\">|<\/span> std<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>take_while(boundary) \n                                                 <span style=\"color: #555555\">|<\/span> std<span style=\"color: #555555\">::<\/span>ranges<span style=\"color: #555555\">::<\/span>to<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>vector<span style=\"color: #555555\">&gt;<\/span>();\n    }\n    <span style=\"color: #006699; font-weight: bold\">else<\/span> {                                              <span style=\"color: #0099FF; font-style: italic\">\/\/ (6)                                  <\/span>\n        begin<span style=\"color: #555555\">++<\/span>;\n        end<span style=\"color: #555555\">++<\/span>;\n        stepsize <span style=\"color: #555555\">*=<\/span> <span style=\"color: #555555\">-<\/span><span style=\"color: #FF6600\">1<\/span>;\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> boundary <span style=\"color: #555555\">=<\/span> [begin](<span style=\"color: #007788; font-weight: bold\">int<\/span> i){ <span style=\"color: #006699; font-weight: bold\">return<\/span> i <span style=\"color: #555555\">&lt;<\/span> begin; };\n        result <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>ranges<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>iota(end) <span style=\"color: #555555\">|<\/span> std<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>take_while(boundary) \n                                               <span style=\"color: #555555\">|<\/span> std<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>reverse \n                                               <span style=\"color: #555555\">|<\/span> std<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>stride(stepsize) \n                                               <span style=\"color: #555555\">|<\/span> std<span style=\"color: #555555\">::<\/span>ranges<span style=\"color: #555555\">::<\/span>to<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>vector<span style=\"color: #555555\">&gt;<\/span>();\n    }\n    <span style=\"color: #006699; font-weight: bold\">return<\/span> result;\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> std<span style=\"color: #555555\">::<\/span>endl;\n\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ range(1, 50)                                 \/\/ (1)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> res <span style=\"color: #555555\">=<\/span> range(<span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">50<\/span>);\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> i<span style=\"color: #555555\">:<\/span> res) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> i <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    \n    <span style=\"color: #0099FF; font-style: italic\">\/\/ range(1, 50, 5)                              \/\/ (2)<\/span>\n    res <span style=\"color: #555555\">=<\/span> range(<span style=\"color: #FF6600\">1<\/span>, <span style=\"color: #FF6600\">50<\/span>, <span style=\"color: #FF6600\">5<\/span>);\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> i<span style=\"color: #555555\">:<\/span> res) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> i <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    \n    <span style=\"color: #0099FF; font-style: italic\">\/\/ range(50, 10, -1)                           \/\/ (3)<\/span>\n    res <span style=\"color: #555555\">=<\/span> range(<span style=\"color: #FF6600\">50<\/span>, <span style=\"color: #FF6600\">10<\/span>, <span style=\"color: #555555\">-<\/span><span style=\"color: #FF6600\">1<\/span>);\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> i<span style=\"color: #555555\">:<\/span> res) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> i <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    \n    <span style=\"color: #0099FF; font-style: italic\">\/\/ range(50, 10, -5)                           \/\/ (4)<\/span>\n    res <span style=\"color: #555555\">=<\/span> range(<span style=\"color: #FF6600\">50<\/span>, <span style=\"color: #FF6600\">10<\/span>, <span style=\"color: #555555\">-<\/span><span style=\"color: #FF6600\">5<\/span>);\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> i<span style=\"color: #555555\">:<\/span> res) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> i <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    \n}\n<\/pre><\/div>\n\n\n\n<p>The lines (1) to (4) should be pretty obvious, thanks to the program&#8217;s output.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/rangeCpp23-1030x513.png\" alt=\"\" class=\"wp-image-8244\" style=\"width:550px\" width=\"550\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/rangeCpp23-1030x513.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/rangeCpp23-300x149.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/rangeCpp23-768x382.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/rangeCpp23-705x351.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/rangeCpp23.png 1362w\" sizes=\"(max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p>The first two arguments of the range call stand for the beginning and end of the created integers. The begin is included, but not the end. The step size as the third parameter is, per default, 1. The step size should be negative when the interval [begin, end] decreases. If not, you get an empty <code>list <\/code>or an empty <code>std::vector&lt;int&gt;<\/code>.<\/p>\n\n\n\n<p>The if condition (<code>begin &lt; end<\/code>) of the range function in line (5) should be quite easy to read. Create all numbers starting with <code>begin (std::views::iota(begin))<\/code>, take each n-th element (<code>std::views::stride(stepsize)<\/code>), and do it as long as the boundary condition holds <code>(std::views::take_while(boundary<\/code>). Finally, create the <code>std::vector&lt;int&gt;<\/code>.<\/p>\n\n\n\n<p>I use a little trick in the other case (line 6). I create the numbers [end++, begin++[, take them until the boundary condition is met, reverse them (<code>std::views::reverse<\/code>), and take each n-th element.<\/p>\n\n\n\n<p>Now, let me jump to first coroutine in C++.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">std::generator <\/h2>\n\n\n\n<p><code>std::generator<\/code> in C++23 is the first concrete coroutine. A <code>std::generator <\/code>generates a sequence of elements by repeatedly resuming the coroutine from which it was paused.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto; gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ generator.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;generator&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;ranges&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n \nstd<span style=\"color: #555555\">::<\/span>generator<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> fib() {\n    co_yield <span style=\"color: #FF6600\">0<\/span>;                    <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> a <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> b <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">1<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">for<\/span>(<span style=\"color: #006699; font-weight: bold\">auto<\/span> n <span style=\"color: #555555\">:<\/span> std<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>iota(<span style=\"color: #FF6600\">0<\/span>)) {  \n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> next <span style=\"color: #555555\">=<\/span> a <span style=\"color: #555555\">+<\/span> b;\n        a <span style=\"color: #555555\">=<\/span> b;\n        b <span style=\"color: #555555\">=<\/span> next;\n        co_yield next;            <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)   <\/span>\n    }\n}\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> main() {\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> f <span style=\"color: #555555\">:<\/span> fib() <span style=\"color: #555555\">|<\/span> std<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>take(<span style=\"color: #FF6600\">10<\/span>)) {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> f <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; &quot;<\/span>;\n    }\n}\n<\/pre><\/div>\n\n\n\n<p>The function <code>fib <\/code>is a coroutine. This coroutine creates an infinite stream of Fibonacci numbers. The stream of numbers starts with 0 (line 1) and continues with the following Fibonacci number (line 2). The ranges-based for-loop requests explicitly the first 10 Fibonacci numbers.<\/p>\n\n\n\n<p>So far, no compiler supports<code> std::generator.<\/code> You can see the coroutine nature of <code>std::generator <\/code>if you study its header:<code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/generator\" data-type=\"link\" data-id=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/generator\">&lt;generator&gt;<\/a>.<\/code> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>std::ranges::elements_of<\/code><\/h3>\n\n\n\n<p><code>std::ranges::elements_of <\/code>comes into play when you want to call a generator recursively.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto; gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">std<span style=\"color: #555555\">::<\/span>generator<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> fib() {\n    co_yield <span style=\"color: #FF6600\">0<\/span>;                    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> a <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> b <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">1<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">for<\/span>(<span style=\"color: #006699; font-weight: bold\">auto<\/span> n <span style=\"color: #555555\">:<\/span> std<span style=\"color: #555555\">::<\/span>views<span style=\"color: #555555\">::<\/span>iota(<span style=\"color: #FF6600\">0<\/span>)) {  \n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> next <span style=\"color: #555555\">=<\/span> a <span style=\"color: #555555\">+<\/span> b;\n        a <span style=\"color: #555555\">=<\/span> b;\n        b <span style=\"color: #555555\">=<\/span> next;\n        co_yield next;               \n    }\n}\n\nstd<span style=\"color: #555555\">::<\/span>generator<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> outer() {\n    yield fib();                         <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n    yield std<span style=\"color: #555555\">::<\/span>ranges<span style=\"color: #555555\">::<\/span>elements_of(fib); <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n}\n<\/pre><\/div>\n\n\n\n<p>The <code>outer <\/code>generator returns in line (1) the inner <code>std::generator&lt;int&gt;<\/code>, but in line (2) the values of the inner generator. Both coroutines have the same return type. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">std::bind_back<\/h2>\n\n\n\n<p>Accordingly, to <code><a href=\"https:\/\/www.modernescpp.com\/index.php\/more-and-more-utilities-in-c-20\/\">std::bind_front<\/a> <\/code>in C++20, C++23 supports <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/functional\/bind_front\" data-type=\"link\" data-id=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/functional\/bind_front\"><code>std::bind_back<\/code><\/a>. The following program <code>bindFrontBack.cpp<\/code> shows the application of both functions. <\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto; gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ bindFrontBack.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;functional&gt;<\/span>\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: #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\">&#39;\\n&#39;<\/span>;\n    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> add <span style=\"color: #555555\">=<\/span> [](std<span style=\"color: #555555\">::<\/span>string a, std<span style=\"color: #555555\">::<\/span>string b, std<span style=\"color: #555555\">::<\/span>string c) { \n        <span style=\"color: #006699; font-weight: bold\">return<\/span> a <span style=\"color: #555555\">+<\/span> b <span style=\"color: #555555\">+<\/span> c;\n    };\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> two_three <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>bind_front(add, <span style=\"color: #CC3300\">&quot;one &quot;<\/span>);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> two_three(<span style=\"color: #CC3300\">&quot;two &quot;<\/span>, <span style=\"color: #CC3300\">&quot;three &quot;<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> one_two <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>bind_back(add, <span style=\"color: #CC3300\">&quot;three &quot;<\/span>);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> one_two(<span style=\"color: #CC3300\">&quot;one &quot;<\/span>, <span style=\"color: #CC3300\">&quot;two &quot;<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;  \n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    \n}\n<\/pre><\/div>\n\n\n\n<p>Here is the output of the program.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/bindFrontBack.png\" alt=\"\" class=\"wp-image-8252\" style=\"width:450px\" width=\"450\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/bindFrontBack.png 965w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/bindFrontBack-300x129.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/bindFrontBack-768x329.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/09\/bindFrontBack-705x302.png 705w\" sizes=\"(max-width: 965px) 100vw, 965px\" \/><\/figure>\n\n\n\n<p>If you want to know more about partial function applications with <code>std::bind<\/code>, <code>std::bind_fron<\/code>t, and <code>std::bind_back<\/code>, read my post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/partial-function-application\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/partial-function-application\/\">Partial Function Application<\/a>&#8220;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s next? <\/h2>\n\n\n\n<p>I&#8217;m done with C++23. Let me jump six years back. In my next post, I will write about an almost unknown feature in C++17: <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/memory\/polymorphic_allocator\" data-type=\"link\" data-id=\"https:\/\/en.cppreference.com\/w\/cpp\/memory\/polymorphic_allocator\">polymorphic allocators<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++20 does not provide concrete coroutines, but C++20 provides a framework for implementing coroutines. This changes with C++23. std::generator is the first concrete coroutine. std::generator is part of the extension of the ranges library in C++23. So, let me start this post with the ranges library in C++20 and its extension in C++23. I will [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":8236,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[378],"tags":[413],"class_list":["post-8234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-23","tag-ranges"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/8234","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=8234"}],"version-history":[{"count":27,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/8234\/revisions"}],"predecessor-version":[{"id":8264,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/8234\/revisions\/8264"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8236"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=8234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=8234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=8234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}