{"id":6002,"date":"2020-10-18T20:55:50","date_gmt":"2020-10-18T20:55:50","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/more-and-more-utilities-in-c-20\/"},"modified":"2020-10-18T20:55:50","modified_gmt":"2020-10-18T20:55:50","slug":"more-and-more-utilities-in-c-20","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/more-and-more-utilities-in-c-20\/","title":{"rendered":"More and More Utilities in C++20"},"content":{"rendered":"<p>Today, I present a few utilities for calculating the midpoint of two values, checking if a <code>std::string<\/code> starts or ends with a substring, and creating callables with<code> std::bind_front<\/code>. These little utilities may not seem so minor when you need them.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5945\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage.png\" alt=\"TimelineCpp20CoreLanguage\" width=\"650\" height=\"262\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>Let&#8217;s start with arithmetical.<\/p>\n<h2>Midpoint and Linear Interpolation<\/h2>\n<ul>\n<li><strong><code>std::midpoint(a, b)<\/code><\/strong> calculates the midpoint<code> (a + (b - a) \/ 2)<\/code> of the integers, floating points, or pointers. If a and b are pointers, they must point to the same array object.<\/li>\n<li><code><strong>std::lerp(a, b, t)<\/strong><\/code> calculates the linear interpolation (a + t( b &#8211; a)). When t is outside the range [0, 1], it calculates the linear extrapolation.<\/li>\n<\/ul>\n<p>The following program applies both functions.<\/p>\n<p>&nbsp;<\/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;\">\/\/ midpointLerp.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;cmath&gt;     <\/span><span style=\"color: #0099ff; font-style: italic;\">\/\/ std::lerp<\/span>\r\n<span style=\"color: #009999;\">#include &lt;numeric&gt;   <\/span><span style=\"color: #0099ff; font-style: italic;\">\/\/ std::midpoint<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::midpoint(10, 20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>midpoint(<span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">20<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\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> {<span style=\"color: #ff6600;\">0.0<\/span>, <span style=\"color: #ff6600;\">0.1<\/span>, <span style=\"color: #ff6600;\">0.2<\/span>, <span style=\"color: #ff6600;\">0.3<\/span>, <span style=\"color: #ff6600;\">0.4<\/span>, <span style=\"color: #ff6600;\">0.5<\/span>, <span style=\"color: #ff6600;\">0.6<\/span>, <span style=\"color: #ff6600;\">0.7<\/span>, <span style=\"color: #ff6600;\">0.8<\/span>, <span style=\"color: #ff6600;\">0.9<\/span>, <span style=\"color: #ff6600;\">1.0<\/span>}) {\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"std::lerp(10, 20, \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>lerp(<span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">20<\/span>, v) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The output of the program should be self-explanatory. If not, try it out on <a href=\"https:\/\/godbolt.org\/z\/Y8qsbz\">Compiler Explorer<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5999\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/midpointLerp.PNG\" alt=\"midpointLerp\" width=\"300\" height=\"349\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/midpointLerp.PNG 376w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/midpointLerp-258x300.png 258w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>C++20 has convenience functions for creating arrays.<\/p>\n<\/p>\n<h2>Creating Arrays and<\/h2>\n<p>With <code>std::to_array, <\/code>and <code>std::make_shared,<\/code> C++20 offers new&nbsp; ways to create a <code>std::array<\/code> or <code>std::shared_ptr<\/code> from C-arrays.<\/p>\n<h3><code>std::to_array<\/code><\/h3>\n<p>Thanks to<code> std::to_array<\/code>, creating a <code>std::array<\/code> from a C-array is a straightforward job.<\/p>\n<p>&nbsp;<\/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;\">\/\/ toArray.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;type_traits&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;utility&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;array&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> arr1 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>to_array(<span style=\"color: #cc3300;\">\"C-String Literal\"<\/span>);\r\n    static_assert(arr1.size() <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">17<\/span>);                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> arr2 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>to_array({ <span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">3<\/span> });         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    static_assert(std<span style=\"color: #555555;\">::<\/span>is_same<span style=\"color: #555555;\">&lt;<\/span>decltype(arr2), std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #ff6600;\">4<\/span><span style=\"color: #555555;\">&gt;&gt;::<\/span>value);\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> arr3 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>to_array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">long<\/span><span style=\"color: #555555;\">&gt;<\/span>({ <span style=\"color: #ff6600;\">0<\/span>, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">3<\/span> });      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n     static_assert(std<span style=\"color: #555555;\">::<\/span>is_same<span style=\"color: #555555;\">&lt;<\/span>decltype(arr3), std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">long<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;&gt;::<\/span>value);\r\n \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> arr4 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>to_array<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>pair<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">float<\/span><span style=\"color: #555555;\">&gt;&gt;<\/span>( { { <span style=\"color: #ff6600;\">3<\/span>, <span style=\"color: #ff6600;\">.0f<\/span> }, { <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">.1f<\/span> }, { <span style=\"color: #ff6600;\">4<\/span>, <span style=\"color: #ff6600;\">.1e23<\/span>f } });\r\n    static_assert(arr4.size() <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">3<\/span>);                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    static_assert(std<span style=\"color: #555555;\">::<\/span>is_same<span style=\"color: #555555;\">&lt;<\/span>decltype(arr4), std<span style=\"color: #555555;\">::<\/span>array<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>pair<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">float<\/span><span style=\"color: #555555;\">&gt;<\/span>, <span style=\"color: #ff6600;\">3<\/span><span style=\"color: #555555;\">&gt;&gt;::<\/span>value);\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The lines (1), (2), (3), and (3) assert that the created<span style=\"font-family: courier new, courier;\"> std::array<\/span> has the expected type and size.<\/p>\n<p>Per design, a <code>std::array<\/code> is as cheap and as fast as a C-array. If you want to know more about<code> std::array<\/code>, and why you should not use a C-array, read my post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/component\/jaggyblog\/std-array-dynamic-memory-no-thanks\">std::array &#8211; Dynamic Memory, no Thanks<\/a>&#8220;.<\/p>\n<p>Additionally, a<code> std::array<\/code> knows its size and supports the typical interface of each container of the Standard Template Library, such as <code>std::vector<\/code>.<\/p>\n<p>So far, all MSVC, Clang, GCC compilers support this convenient way to create a <span style=\"font-family: courier new, courier;\">std::array<\/span>. This observation does not hold for the next feature.<\/p>\n<h3><code>Create a std::shared_ptr of C-arrays<\/code><code><br \/><\/code><\/h3>\n<p>Since C++11, C++ has the factory function <code>std::make_shared<\/code> to create a <code>std::shared_ptr<\/code>. Since C++20, <code>std::make_shared<\/code> also supports the creation of <code>std::shared_ptr<\/code> of C-arrays.<\/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;\">auto<\/span> s1 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_shared<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>[]<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">1024<\/span>);\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> s2 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_shared<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>[]<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #ff6600;\">1024<\/span>, <span style=\"color: #ff6600;\">1.0<\/span>);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><code>s1<\/code> is a <code>std::shared_ptr <\/code>of a C-array. All members are default initialized. s2 is a <code>std::shared_ptr<\/code> of a C-array. Each element is initialized to <code>1.0.<\/code><\/p>\n<p>In contrast, the new two new member functions of <code>std::string<\/code> are already available with a brand-new MSVC, Clang, or GCC compiler.<\/p>\n<h2>Check if a String starts with a Prefix or ends with a Suffix<\/h2>\n<p><code>std::string<\/code> get a new member functions <code>starts_with<\/code> and<code> ends_with<\/code> which checks if a <code>std::string<\/code> start or ends with a specified substring<\/p>\n<p>&nbsp;<\/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;\">\/\/ stringStartsWithEndsWith.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string_view&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n \r\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> PrefixType<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> startsWith(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> str, PrefixType prefix) {\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"            starts with \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> prefix <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\": \"<\/span> \r\n\t          <span style=\"color: #555555;\">&lt;&lt;<\/span> str.starts_with(prefix) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n}\r\n\r\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> SuffixType<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> endsWith(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> str, SuffixType suffix) {\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"            ends with \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> suffix <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\": \"<\/span> \r\n\t          <span style=\"color: #555555;\">&lt;&lt;<\/span> str.ends_with(suffix) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n}\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha;    \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>string helloWorld(<span style=\"color: #cc3300;\">\"Hello World\"<\/span>);\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> helloWorld <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n    startsWith(helloWorld, helloWorld);                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n \r\n    startsWith(helloWorld, std<span style=\"color: #555555;\">::<\/span>string_view(<span style=\"color: #cc3300;\">\"Hello\"<\/span>));  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n \r\n    startsWith(helloWorld, <span style=\"color: #cc3300;\">'H'<\/span>);                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    \r\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>; \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> helloWorld <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n    endsWith(helloWorld, helloWorld);\r\n \r\n    endsWith(helloWorld, std<span style=\"color: #555555;\">::<\/span>string_view(<span style=\"color: #cc3300;\">\"World\"<\/span>));\r\n \r\n    endsWith(helloWorld, <span style=\"color: #cc3300;\">'d'<\/span>);\r\n \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Both member functions <code>starts_with<\/code> end <code>ends_with<\/code> are predicates. This means they return a boolean. You can invoke the member function<code> starts_with<\/code> (line 1) with a<code> std::string<\/code> (line 2), a <code>std::string_view<\/code> (line 3), and a <code>char<\/code> (line 4).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6000\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/stringStartsWithEndsWith.png\" alt=\"stringStartsWithEndsWith\" width=\"400\" height=\"260\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/stringStartsWithEndsWith.png 552w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/stringStartsWithEndsWith-300x195.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The following utility function in C++20 may wonder you.<\/p>\n<h2>std::bind_front<\/h2>\n<p><code>std::bind_front (Func&amp;&amp; func, Args&amp;&amp; ... args<\/code>) creates a callable wrapper for a callable<code> func. std::bind_front<\/code> that can have an arbitrary number of arguments and binds its arguments to the front.<\/p>\n<p>Now, to the part which may wonder you. Since C++11, we have<code> std::bind<\/code> and lambda expression. To be pedantic<code> std::bind<\/code> is available since <a href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B_Technical_Report_1\">Technical Report 1<\/a> (TR1). Both can be used as a replacement of<code> std::bind_front<\/code>. Furthermore, <code>std::bind_front<\/code> seems like the minor sister of <code>std::bind,<\/code> because <code>std::bind<\/code> only supports the rearranging of arguments. Of course, there is a reason in the future to use <code>std::bind_front:<\/code>&nbsp;<code>std::bind_front<\/code> propagates exception specification of the underlying call operator.<\/p>\n<p>The following program exemplifies that you can replace <code>std::bind_front <\/code>with<code>&nbsp;<\/code><code>std::bind,<\/code> or lambda expressions.<\/p>\n<p>&nbsp;<\/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;\">\/\/ bindFront.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;functional&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">plusFunction<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> a, <span style=\"color: #007788; font-weight: bold;\">int<\/span> b) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> a <span style=\"color: #555555;\">+<\/span> b;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> plusLambda <span style=\"color: #555555;\">=<\/span> [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> a, <span style=\"color: #007788; font-weight: bold;\">int<\/span> b) {\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> a <span style=\"color: #555555;\">+<\/span> b;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> twoThousandPlus1 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>bind_front(plusFunction, <span style=\"color: #ff6600;\">2000<\/span>);         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"twoThousandPlus1(20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> twoThousandPlus1(<span style=\"color: #ff6600;\">20<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> twoThousandPlus2 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>bind_front(plusLambda, <span style=\"color: #ff6600;\">2000<\/span>);  <span style=\"color: #0099ff; font-style: italic;\">         \/\/ (2)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"twoThousandPlus2(20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> twoThousandPlus2(<span style=\"color: #ff6600;\">20<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> twoThousandPlus3 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>bind_front(std<span style=\"color: #555555;\">::<\/span>plus<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>(), <span style=\"color: #ff6600;\">2000<\/span>);     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"twoThousandPlus3(20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> twoThousandPlus3(<span style=\"color: #ff6600;\">20<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\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>;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std<span style=\"color: #555555;\">::<\/span>placeholders;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> twoThousandPlus4 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>bind(plusFunction, <span style=\"color: #ff6600;\">2000<\/span>, _1);           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"twoThousandPlus4(20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> twoThousandPlus4(<span style=\"color: #ff6600;\">20<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> twoThousandPlus5 <span style=\"color: #555555;\">=<\/span>  [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> b) { <span style=\"color: #006699; font-weight: bold;\">return<\/span> plusLambda(<span style=\"color: #ff6600;\">2000<\/span>, b); };  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span><span style=\"color: #0099ff; font-style: italic;\"><\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"twoThousandPlus5(20): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> twoThousandPlus5(<span style=\"color: #ff6600;\">20<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n       \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Each call (lines 1 &#8211; 5) gets a callable taking two arguments and returns a callable taking only one argument because the first argument is bound to <code>2000<\/code>. The callable is a function (1), a lambda expression (2), and a predefined function object (line 3). <code>_1<\/code> is a so-called placeholder (line 4) and stands for the missing argument. With lambda expression (line 5), you can directly apply one argument and provide an argument <code>b<\/code> for the missing parameter. From the readability perspective,<code> std::bind_front<\/code> is easier to read than <code>std::bind<\/code>, or the lambda expression.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6001\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/bindFront.png\" alt=\"bindFront\" width=\"300\" height=\"199\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/bindFront.png 379w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/10\/bindFront-300x199.png 300w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>If you want to play with the example, use <a href=\"https:\/\/godbolt.org\/z\/bhY3MW\">Compiler Explorer<\/a>.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In my<a href=\"https:\/\/www.modernescpp.com\/index.php\/calendar-and-time-zone-in-c-20\"> next post<\/a> to C++20, I present the extensions of the <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\">chrono <\/a>library: time of day, a calendar, and time zones.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>&nbsp;<\/strong><\/p>\n<\/p>\n<div id=\"simple-translate\">&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today, I present a few utilities for calculating the midpoint of two values, checking if a std::string starts or ends with a substring, and creating callables with std::bind_front. These little utilities may not seem so minor when you need them.<\/p>\n","protected":false},"author":21,"featured_media":5945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[],"class_list":["post-6002","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6002","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=6002"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6002\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5945"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}