{"id":6495,"date":"2023-01-08T10:24:56","date_gmt":"2023-01-08T10:24:56","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/partial-function-application\/"},"modified":"2023-08-20T14:35:37","modified_gmt":"2023-08-20T14:35:37","slug":"partial-function-application","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/partial-function-application\/","title":{"rendered":"Partial Function Application"},"content":{"rendered":"<p>Partial Function Application is a technique in which a function binds a few of its arguments and returns a function taking fewer arguments. This technique is related to a technique used in functional languages called currying.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6493\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/12\/GeneralIdioms.png\" alt=\"GeneralIdioms\" width=\"650\" height=\"331\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/12\/GeneralIdioms.png 1233w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/12\/GeneralIdioms-300x153.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/12\/GeneralIdioms-1024x521.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/12\/GeneralIdioms-768x391.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>A few weeks ago, I had a discussion with a few of my readers. One reader said that I should write about Partial Function Applications. Another reader mentioned that C++ does not support function applications. This is wrong. C++ supports Partial Function Application. Consequently, I am writing today about<code> std::function<\/code>,<code> std::bind<\/code>,<code> std::bind_front<\/code>, lambdas, <code>auto<\/code>, and currying.<\/p>\n<p>Let me start with a bit of theory.<\/p>\n<h2>Currying<\/h2>\n<p>Partial Function application is quite similar to a technique called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Currying\">currying<\/a>. Currying is a well-known technique used in functional languages such as&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Haskell_(programming_language\">Haskell<\/a>. It stands for a technique in which a function that takes more than one argument will successively be transformed into a series of functions taking only one argument. Therefore, a programming language such as Haskell has only functions taking one argument. I hear your question. How is it possible to implement a function such as <code>add<\/code> which needs two arguments? The magic is happening implicitly. Functions that need n arguments are transformed into functions returning a function that only needs n -1 arguments. The first element is evaluated in this transformation.<\/p>\n<p>The name currying is coined by the mathematician <a href=\"https:\/\/en.wikipedia.org\/wiki\/Haskell_Curry\">Haskell Curry <\/a>and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Moses_Sch%C3%B6nfinkel\">Moses Sch\u00f6nfinkel.<\/a> Currying is named after the family name of Haskell Curry; Haskell after his first name. Sometimes, currying is also called sch\u00f6nfinkeln.<\/p>\n<p>Partial Function Application is more powerful than currying because you can evaluate arbitrary function arguments. Since C++11, C++ supports<code> std::function<\/code> and <code>std::bind<\/code>.<\/p>\n<h2><code>std::bind<\/code><\/h2>\n<p><code>std::bind<\/code> enables you to create callables in various ways. You can<\/p>\n<ul>\n<li>bind function argument at arbitrary positions,<\/li>\n<li>reorder the sequence of the function arguments,<\/li>\n<li>introduce placeholders for function arguments,<\/li>\n<li>partially evaluate functions.<\/li>\n<\/ul>\n<p>Furthermore, you can<\/p>\n<ul>\n<li>directly invoke the new callable,<\/li>\n<li>use the callable in an algorithm of the Standard Template Library (STL),<\/li>\n<li>store the callable in <code>std::function<\/code>.<\/li>\n<\/ul>\n<p>Before I show you an example, I have to introduce <code>std::function<\/code>:<\/p>\n<ul>\n<li><code>std::function<\/code> is a polymorphic function wrapper. It can take arbitrary callables and give them a name. Callables are all entities that behave like a function. In particular, these are lambda expressions, function objects, or functions themselves. <code>std::function<\/code> is required if you have to specify the type of a callable.<\/li>\n<\/ul>\n<p>Now, I&#8217;m done with the theory and can show an example of Partial Function Application:<\/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: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ bindAndFunction.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;\">double<\/span> <span style=\"color: #cc00ff;\">divMe<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span> a, <span style=\"color: #007788; font-weight: bold;\">double<\/span> b){\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #007788; font-weight: bold;\">double<\/span>(a<span style=\"color: #555555;\">\/<\/span>b);\r\n}\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;                           <em><span style=\"color: #0099ff;\">            \/\/ (1)<\/span><\/em>\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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ invoking the function object directly<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>bind(divMe, <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2.0<\/span>)() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;          <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ placeholders for both arguments                                     \/\/ (3)<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>function<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #007788; font-weight: bold;\">double<\/span>)<span style=\"color: #555555;\">&gt;<\/span> myDivBindPlaceholder<span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>bind(divMe, _1, _2);\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myDivBindPlaceholder(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2.0<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ placeholders for both arguments, swap the arguments                 \/\/ (4)<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>function<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #007788; font-weight: bold;\">double<\/span>)<span style=\"color: #555555;\">&gt;<\/span> myDivBindPlaceholderSwap<span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>bind(divMe, _2, _1);\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myDivBindPlaceholderSwap(<span style=\"color: #ff6600;\">2.0<\/span>, <span style=\"color: #ff6600;\">1<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ placeholder for the first argument                                 \/\/ (5)<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>function<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span>)<span style=\"color: #555555;\">&gt;<\/span> myDivBind1St<span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>bind(divMe, _1, <span style=\"color: #ff6600;\">2.0<\/span>);\r\n  std<span style=\"color: #555555;\">::<\/span>cout<span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myDivBind1St(<span style=\"color: #ff6600;\">1<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ placeholder for the second argument                                \/\/ (6)<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>function<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span>)<span style=\"color: #555555;\">&gt;<\/span> myDivBind2Nd<span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>bind(divMe, <span style=\"color: #ff6600;\">1.0<\/span>, _1);\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myDivBind2Nd(<span style=\"color: #ff6600;\">2.0<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In order to use the simple notation <code>_1, _2<\/code>&nbsp;for the placeholders <code>std::placeholders::_1, std::placeholders::_2<\/code> in the source code, I have to introduce the namespace <code>std::placeholders<\/code> in line 1.<\/p>\n<p>I bind in line 2 in the expression <code>std::bind(divMe, 1, 2.0)<\/code> the arguments<code> 1<\/code> and <code>2.0<\/code> to the function <code>divMe<\/code> and invoke them in place. Lines 3, 4, 5, and 6 follow a similar strategy, but I give the created callables a name using<code> std::function<\/code>, and invoke them finally. A template signature like<code> double(double, double)<\/code> (line 4) or<code> double(double)<\/code> (lines 5 and 6) stands for the type of callable that<code> std::function<\/code> accepts<code>. double(double, double)<\/code> is a function taking two doubles and returning a double.<\/p>\n<p>In particular, the last two examples (lines 5 and 6) in which <code>std::function<\/code> gets a function of arity <strong>two<\/strong> and returns a function of arity <strong>one<\/strong> is quite astonishing. The arity of a function is the number of arguments a function gets.<code> std::bind<\/code> evaluates in both calls only one argument and uses for the non-evaluated one a placeholder. This technique is called Partial Function Application.<\/p>\n<p>In the end, here is the output of the program.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5128\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/bindAndFunction.png\" alt=\"bindAndFunction\" width=\"447\" height=\"244\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/bindAndFunction.png 447w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/bindAndFunction-300x164.png 300w\" sizes=\"auto, (max-width: 447px) 100vw, 447px\" \/><\/p>\n<p>There is another way in C++11 to use Partial Function Application: lambda expressions.<\/p>\n<\/p>\n<h2>Lambda Expressions<\/h2>\n<p><code>std::bind<\/code> and <code>std::function<\/code> are almost superfluous with C++11. You can use lambda expressions instead of<code> std::bind<\/code>,&nbsp; and almost always <code>auto<\/code> instead of <code>std::function<\/code>. Here is the equivalent program based on <code>auto<\/code>, and lambda expressions.<\/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;\">\/\/ lambdaAndAuto.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;\">double<\/span> <span style=\"color: #cc00ff;\">divMe<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span> a, <span style=\"color: #007788; font-weight: bold;\">double<\/span> b){\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #007788; font-weight: bold;\">double<\/span>(a<span style=\"color: #555555;\">\/<\/span>b);\r\n}\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: #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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ invoking the function object directly<\/span>\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> a, <span style=\"color: #007788; font-weight: bold;\">int<\/span> b){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> divMe(a, b); }(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2.0<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ placeholders for both arguments<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> myDivBindPlaceholder<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){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> divMe(a, b); };\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myDivBindPlaceholder(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2.0<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ placeholders for both arguments, swap the arguments<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> myDivBindPlaceholderSwap<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){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> divMe(b, a); };\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myDivBindPlaceholderSwap(<span style=\"color: #ff6600;\">2.0<\/span>, <span style=\"color: #ff6600;\">1<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ placeholder for the first argument<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> myDivBind1St<span style=\"color: #555555;\">=<\/span> [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> a){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> divMe(a, <span style=\"color: #ff6600;\">2.0<\/span>); };\r\n  std<span style=\"color: #555555;\">::<\/span>cout<span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myDivBind1St(<span style=\"color: #ff6600;\">1<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  <span style=\"color: #0099ff; font-style: italic;\">\/\/ placeholder for the second argument<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">auto<\/span> myDivBind2Nd<span style=\"color: #555555;\">=<\/span> [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> b){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> divMe(<span style=\"color: #ff6600;\">1<\/span>, b); };\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\/2.0= \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> myDivBind2Nd(<span style=\"color: #ff6600;\">2.0<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Let me say write a few words about the lambda expressions. The expression<code> [](int a, int b){ return divMe(a, b); }(1, 2.0)<\/code> defines a lambda exepression that executes <code>divMe<\/code>. The trailing braces invoke the lambda expression just in place using the arguments <code>1<\/code> and <code>2.0<\/code>. On the contrary, the remaining lambda expressions are invoked in the subsequent lines. You can use a lambda expression to bind any argument of the underlying function.<\/p>\n<p>So far, I have applied Partial Function application with <code>std::bind<\/code> and lambda expressions. In C++20, there is a new variation of<code> std::bind<\/code>:<\/p>\n<h2><code>std::bind_front<\/code><\/h2>\n<p><code>std::bind_front <\/code>creates a callable<code>. std::bind_front<\/code> can have an arbitrary number of arguments and binds its arguments to the front. You may wonder why we have<code> std::bind_front<\/code> because we have since C++11<code> std::bind<\/code>, which can also bind to the front. Here is the point. First, <code>std::bind_front<\/code> is easier to use because it does not need placeholders, and second,&nbsp; <code>std::bind_front<\/code> propagates an exception specification of the underlying callable.<\/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><!-- 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: 0px; 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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\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>);            <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\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;\"><em>\/\/ (2<\/em>)<\/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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\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>);        <em><span style=\"color: #0099ff;\">\/\/ (3)<\/span><\/em>\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> <span style=\"color: #cc3300;\">'\\n'<\/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    <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);            <em> <span style=\"color: #0099ff;\">\/\/ (4)<\/span><\/em>\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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\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); };   <em> <span style=\"color: #0099ff;\">\/\/ (5)<\/span><\/em>\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> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n       \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\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> 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. Regarding readability, is<code> std::bind_front<\/code> easier to use than <code>std::bind<\/code>, or a 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=\"408\" height=\"325\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>Argument-Dependent Lookup (ADL), also known as Koening Lookup&nbsp;is a set of &#8220;magical&#8221; rules for the lookup of unqualified functions based on their function arguments.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Partial Function Application is a technique in which a function binds a few of its arguments and returns a function taking fewer arguments. This technique is related to a technique used in functional languages called currying.<\/p>\n","protected":false},"author":21,"featured_media":6493,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[459],"class_list":["post-6495","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-lambdas"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6495","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=6495"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6495\/revisions"}],"predecessor-version":[{"id":8106,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6495\/revisions\/8106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6493"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}