{"id":5129,"date":"2017-01-17T06:59:09","date_gmt":"2017-01-17T06:59:09","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/functional-in-tr1-and-c-11\/"},"modified":"2023-06-26T12:26:55","modified_gmt":"2023-06-26T12:26:55","slug":"functional-in-tr1-and-c-11","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/functional-in-tr1-and-c-11\/","title":{"rendered":"Functional in TR1 and C++11"},"content":{"rendered":"<p>This post continues our journey through the functional features of classical, modern, and future C++. Today, we stop in the present.<\/p>\n<p><!--more--><\/p>\n<h2>&nbsp;What has modern C++ to offer?<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5127\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/FunktionalTR1Cpp11Eng.png\" alt=\"FunktionalTR1Cpp11Eng\" width=\"700\" height=\"335\" style=\"margin: 15px auto; display: block;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/FunktionalTR1Cpp11Eng.png 952w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/FunktionalTR1Cpp11Eng-300x143.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/FunktionalTR1Cpp11Eng-768x367.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>In 2005, 13 libraries based on Boost were proposed in the so-called <a href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B_Technical_Report_1\">technical report 1 <\/a>(TR1) as candidates for the upcoming C++ standard. 12 of them made it into C++11 including the two functions <span style=\"font-family: courier new,courier;\">std::bind<\/span> and <span style=\"font-family: courier new,courier;\">std::function<\/span>. Both functions work very well together. On the one hand, <span style=\"font-family: courier new,courier;\">std::bind<\/span> empowers you to make functional objects easily composable. On the other hand, <span style=\"font-family: courier new,courier;\">std::functions<\/span> get these temporary function objects from <span style=\"font-family: courier new,courier;\">std::bind<\/span> and give them a name. Both functions need the header<span style=\"font-family: courier new,courier;\"> <\/span><span style=\"font-family: courier new,courier;\">&lt;functional&gt;<\/span>. You can guess why.<\/p>\n<h3>std::bind<\/h3>\n<p>You can generate with <span style=\"font-family: courier new,courier;\">std::bind<\/span> function objects in various ways because it empowers you to<\/p>\n<ul>\n<li>bind the argument at arbitrary positions,<\/li>\n<li>reorder the sequence of the arguments,<\/li>\n<li>introduce placeholders for arguments,<\/li>\n<li>partially evaluate functions,<\/li>\n<li>and directly invoke the new function object, use it in the Standard Template Library (STL) algorithm, or store it in <span style=\"font-family: courier new,courier;\">std::function.<\/span><\/li>\n<\/ul>\n<h3>std::function<\/h3>\n<p><span style=\"font-family: courier new,courier;\">std::function<\/span> is a polymorphic function wrapper. Therefore, it can take arbitrary callables and give them a name.&nbsp; Callables are all entities that behave like a function. In particular, these are lambda functions, function objects, and functions themselves. <span style=\"font-family: courier new,courier;\">std::function<\/span> is always needed if you have to specify the callable type.<\/p>\n<p>That was enough theory. Let&#8217;s start with the more exciting stuff.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ bindAndFunction.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;functional&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">double<\/span> divMe(<span style=\"color: #2b91af;\">double<\/span> a, <span style=\"color: #2b91af;\">double<\/span> b){\r\n  <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #2b91af;\">double<\/span>(a\/b);\r\n}\r\n\r\n<span style=\"color: #0000ff;\">using<\/span> <span style=\"color: #0000ff;\">namespace<\/span> std::placeholders;\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ invoking the function object directly<\/span>\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; std::bind(divMe, 1, 2.0)() &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ placeholders for both arguments<\/span>\r\n  std::function&lt;<span style=\"color: #2b91af;\">double<\/span>(<span style=\"color: #2b91af;\">double<\/span>, <span style=\"color: #2b91af;\">double<\/span>)&gt; myDivBindPlaceholder= std::bind(divMe, _1, _2);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; myDivBindPlaceholder(1, 2.0) &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ placeholders for both arguments, swap the arguments<\/span>\r\n  std::function&lt;<span style=\"color: #2b91af;\">double<\/span>(<span style=\"color: #2b91af;\">double<\/span>, <span style=\"color: #2b91af;\">double<\/span>)&gt; myDivBindPlaceholderSwap= std::bind(divMe, _2, _1);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; myDivBindPlaceholderSwap(2.0, 1) &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ placeholder for the first argument<\/span>\r\n  std::function&lt;<span style=\"color: #2b91af;\">double<\/span>(<span style=\"color: #2b91af;\">double<\/span>)&gt; myDivBind1St= std::bind(divMe, _1, 2.0);\r\n  std::cout&lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; myDivBind1St(1) &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ placeholder for the second argument<\/span>\r\n  std::function&lt;<span style=\"color: #2b91af;\">double<\/span>(<span style=\"color: #2b91af;\">double<\/span>)&gt; myDivBind2Nd= std::bind(divMe, 1.0, _1);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; myDivBind2Nd(2.0) &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Do you want to see a few variations of invoking <span style=\"font-family: courier new,courier;\">divMe<\/span> (lines 6 &#8211; 8)? To use the simple notation<span style=\"font-family: courier new,courier;\"> _1, _2&nbsp;<\/span>for the placeholders <span style=\"font-family: courier new,courier;\">std::placeholders::_1, std::placeholders::_2<\/span> in the source code, I have to introduce the namespace <span style=\"font-family: courier new,courier;\">std::placeholders<\/span> in line 10.<\/p>\n<p>I bind in line 17 in the expression <span style=\"font-family: courier new,courier;\">std::bind(divMe, 1, 2.0)<\/span> the arguments 1 and 2.0 to the function <span style=\"font-family: courier new,courier;\">divMe<\/span> and invoke them in place with (). I bind in line 20 the function object and invoke it with arguments 1 and 2.0. Line 24, 28, and 32 follows a similar strategy. I change the sequence of the arguments in line 24. In line 28, only the first argument is bound; in line 32, only the second argument is. <span style=\"font-family: courier new,courier;\">std::function<\/span> gets the resulting function objects.&nbsp; Template arguments like <span style=\"font-family: courier new,courier;\">double(double, double)<\/span> (line 24) or <span style=\"font-family: courier new,courier;\">double(double)<\/span> (line 28 and 32) stands for the the callable, which <span style=\"font-family: courier new,courier;\">std::function<\/span> accepts. <span style=\"font-family: courier new,courier;\">double(double, double)<\/span> is a function taking two <span style=\"font-family: courier new,courier;\">doubles<\/span> and returning a <span style=\"font-family: courier new,courier;\">double. <\/span><\/p>\n<p>At the end, 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\" style=\"margin: 15px;\" width=\"447\" height=\"244\" 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>Impressed? I guess, yes. In particular, the last two examples in which <span style=\"font-family: courier new,courier;\">std::function<\/span> gets a function of arity <strong>two<\/strong> and returns a function of arity <strong>one<\/strong> are quite astonishing. The arity of a function is the number of arguments a function gets. <span style=\"font-family: courier new,courier;\">std::bind<\/span> 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<h3>Partial function application<\/h3>\n<p>Partial function application is quite similar to a technique called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Currying\">currying<\/a>. Currying is well-known in functional programming. It stands for a technique in which a function takes more than one argument and will successively be transformed into a series of functions taking only one argument. Therefore, a function takes only one argument in the programming language Haskell. I hear your question. How can a function such as add be implemented, which needs two arguments? The magic is happening implicitly. Functions in Haskell that needs n arguments are transformed into functions returning a function that only needs n-1 arguments. The first elements are 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 called sch\u00f6nfinkeln.<\/p>\n<h2>A drop of bitterness<\/h2>\n<p>A drop of bitterness remains. As well, <span style=\"font-family: courier new,courier;\">std::bind<\/span> and <span style=\"font-family: courier new,courier;\">std::function<\/span> are almost superfluous with C++11. You can use lambda functions instead of <span style=\"font-family: courier new,courier;\">std::bind<\/span>; you can use most of the time automatic type deduction with <span style=\"font-family: courier new,courier;\">auto<\/span> instead of <span style=\"font-family: courier new,courier;\">std::function<\/span>. We can quite efficiently rewrite the program with the enriched core language in C++11.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ lambdaAndAuto.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;functional&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">double<\/span> divMe(<span style=\"color: #2b91af;\">double<\/span> a, <span style=\"color: #2b91af;\">double<\/span> b){\r\n  <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #2b91af;\">double<\/span>(a\/b);\r\n}\r\n\r\n<span style=\"color: #0000ff;\">using<\/span> <span style=\"color: #0000ff;\">namespace<\/span> std::placeholders;\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ invoking the function object directly<\/span>\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; [](<span style=\"color: #2b91af;\">int<\/span> a, <span style=\"color: #2b91af;\">int<\/span> b){ <span style=\"color: #0000ff;\">return<\/span> divMe(a, b); }(1, 2.0) &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ placeholders for both arguments<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> myDivBindPlaceholder= [](<span style=\"color: #2b91af;\">int<\/span> a, <span style=\"color: #2b91af;\">int<\/span> b){ <span style=\"color: #0000ff;\">return<\/span> divMe(a, b); };\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; myDivBindPlaceholder(1, 2.0) &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ placeholders for both arguments, swap the arguments<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> myDivBindPlaceholderSwap= [](<span style=\"color: #2b91af;\">int<\/span> a, <span style=\"color: #2b91af;\">int<\/span> b){ <span style=\"color: #0000ff;\">return<\/span> divMe(b, a); };\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; myDivBindPlaceholderSwap(2.0, 1) &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ placeholder for the first argument<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> myDivBind1St= [](<span style=\"color: #2b91af;\">int<\/span> a){ <span style=\"color: #0000ff;\">return<\/span> divMe(a, 2.0); };\r\n  std::cout&lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; myDivBind1St(1) &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #008000;\">\/\/ placeholder for the second argument<\/span>\r\n  <span style=\"color: #0000ff;\">auto<\/span> myDivBind2Nd= [](<span style=\"color: #2b91af;\">int<\/span> b){ <span style=\"color: #0000ff;\">return<\/span> divMe(1, b); };\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"1\/2.0= \"<\/span> &lt;&lt; myDivBind2Nd(2.0) &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Let me say a few words about the lambda-functions. The expression <span style=\"font-family: courier new,courier;\">[](int a, int b){ return divMe(a, b); }(1, 2.0) <\/span>defines a lambda function that executes <span style=\"font-family: courier new,courier;\"><span style=\"font-family: courier new,courier;\">divMe<\/span><\/span>. The trailing braces invoke the lambda function just in place. This will not hold for the lambda functions in lines 28 and 32. Both will be invoked in the subsequent lines. The impressive fact is that the lambda function binds the first (line 32) or the second argument (line 28).<span style=\"font-family: courier new,courier;\"><br \/><\/span><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The polymorph function wrapper <span style=\"font-family: courier new,courier;\">std::function<\/span> can, most of the time, be replaced by <span style=\"font-family: courier new,courier;\">auto<\/span>. Most of the time because sometimes you have the explicitly specify the callable. That is the domain of <span style=\"font-family: courier new,courier;\">std::function.<\/span> A typical example of such a use case is a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dispatch_table\">dispatch table <\/a>which I will present in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/functional-in-c-dispatch-table\">next post.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post continues our journey through the functional features of classical, modern, and future C++. Today, we stop in the present.<\/p>\n","protected":false},"author":21,"featured_media":5127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[365],"tags":[436],"class_list":["post-5129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-functional","tag-auto"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5129","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=5129"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5129\/revisions"}],"predecessor-version":[{"id":6898,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5129\/revisions\/6898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5127"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}