{"id":5126,"date":"2017-01-15T09:16:20","date_gmt":"2017-01-15T09:16:20","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/functional-in-c-98\/"},"modified":"2017-01-15T09:16:20","modified_gmt":"2017-01-15T09:16:20","slug":"functional-in-c-98","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/functional-in-c-98\/","title":{"rendered":"Functional in C++98"},"content":{"rendered":"<p>C++ is not a functional programming language, but you can program in a functional style. What are the functional features of C++? I will answer this question for C++98.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>Specifically, I want to answer three questions in this and the following posts.<\/p>\n<ul>\n<li>Which functional features does C++ support?<\/li>\n<li>Which functional features do we get with C++17?<\/li>\n<li>What functional features can we hope for with C++20?<\/li>\n<\/ul>\n<h2>The overview<\/h2>\n<p>To get an overview of all functional features in classical, modern, and future C++ a timeline helps a lot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5120\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/timeline.FunktionalEng.png\" alt=\"timeline.FunktionalEng\" width=\"700\" height=\"283\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/timeline.FunktionalEng.png 949w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/timeline.FunktionalEng-300x121.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/timeline.FunktionalEng-768x310.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>The story of functional programming began with the first standard C++98. So, C++98 is the topic of this post.<\/p>\n<h2>C++98<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5121\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/timeline.Funktional98Eng.png\" alt=\"timeline.Funktional98Eng\" width=\"700\" height=\"340\" style=\"margin: 15px;\" \/><\/p>\n<h3>Template metaprogramming<\/h3>\n<p>Template Metaprogramming is programming with templates at compile time. Template instantiation generates the temporary source code that the compiler uses to generate the executable. The graphic shows this process for the class template <span style=\"font-family: courier new,courier;\">Factorial.<\/span><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5122\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/TemplateInstanziierungEng.png\" alt=\"TemplateInstanziierungEng\" width=\"500\" height=\"133\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/TemplateInstanziierungEng.png 834w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/TemplateInstanziierungEng-300x80.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/TemplateInstanziierungEng-768x204.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The <span style=\"font-family: courier new,courier;\">Factorial<\/span> class template is the <strong>Hello World<\/strong> template metaprogramming, and it must not be missing in a post that mentions template metaprogramming.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ factorial.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #2b91af;\">int<\/span> N&gt;\r\n<span style=\"color: #0000ff;\">struct<\/span> Factorial{\r\n  <span style=\"color: #0000ff;\">static<\/span> <span style=\"color: #2b91af;\">int<\/span> <span style=\"color: #0000ff;\">const<\/span> value= N * Factorial&lt;N-1&gt;::value;\r\n};\r\n\r\n<span style=\"color: #0000ff;\">template<\/span> &lt;&gt;\r\n  <span style=\"color: #0000ff;\">struct<\/span> Factorial&lt;1&gt;{\r\n  <span style=\"color: #0000ff;\">static<\/span> <span style=\"color: #2b91af;\">int<\/span> <span style=\"color: #0000ff;\">const<\/span> value = 1;\r\n};\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  std::cout &lt;&lt; \"Factorial&lt;4&gt;::value: \" &lt;&lt; Factorial&lt;4&gt;::value &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; \"Factorial&lt;5&gt;::value: \" &lt;&lt; Factorial&lt;5&gt;::value &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>The program gives the expected result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5123\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/factorial.png\" alt=\"factorial\" style=\"margin: 15px;\" width=\"279\" height=\"195\" \/>&nbsp;<\/p>\n<p>Right, it&#8217;s not so thrilling that the program calculates the correct result. But it is thrilling what is happening under the hood.<\/p>\n<p>Template metaprogramming is a pure functional programming language, which is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Turing_completeness\">turing complete<\/a>.&nbsp; That means template metaprogramming is, in a strict sense a functional programming language (pure functional) and you can calculate all that is calculable (turing complete).<\/p>\n<p>What is key for a pure functional programming language will be the topic of a future post. Only that much. Because of the template instantiation <span style=\"font-family: courier new,courier;\">Factorial&lt;5&gt;::value<\/span> <span>in line 20, the class template (line 5 &#8211; 8) will be invoked and therefore, the class template<\/span> <span style=\"font-family: courier new,courier;\">Factorial&lt;4&gt;::value<\/span> in line 7. This recursion ends with <span style=\"font-family: courier new,courier;\">Factorial&lt;0&gt;::value.<\/span> In this case the fully specialized class template in lines 10 &#8211; 13 kicks in.<\/p>\n<p>To show it in a graphic, the template instantiation of <span style=\"font-family: courier new,courier;\">Factorial&lt;5&gt;::value<\/span> starts at compile time, a recursive process in such a way that the calculation result is already available at compile-time.&nbsp; From the perspective of the executable, it will make no difference if I use <span style=\"font-family: courier new,courier;\">Factorial&lt;5&gt;::value<\/span> or <span style=\"font-family: courier new,courier;\">120<\/span> in <span style=\"font-family: courier new,courier;\">std::cout.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5124\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/factorialInstanziation.png\" alt=\"factorialInstanziation\" width=\"500\" height=\"105\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/factorialInstanziation.png 799w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/factorialInstanziation-300x63.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/factorialInstanziation-768x161.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>What are the functional characteristics of template metaprogramming, which you can observe in the class template <span style=\"font-family: courier new,courier;\">Factorial.<\/span><\/p>\n<p><span style=\"font-family: courier new,courier;\">Factorial<\/span> has no state and no mutation and uses recursion. That should be enough for template metaprogramming.&nbsp;<\/p>\n<\/p>\n<h3>STL algorithm<\/h3>\n<p>What have the algorithm of the standard template library (STL) with functional programming in common? A lot. At first, one of its fathers <a href=\"https:\/\/en.wikipedia.org\/wiki\/Alexander_Stepanov\">Alexander Stephanov<\/a> was a fan of functional programming; second, many of the algorithms of the STL are so-called higher-order functions. Higher-order functions are one of the key characteristics of functional programming.<\/p>\n<p>Higher-order functions that can get functions as an argument or return functions.<\/p>\n<p>In particular, the property that the STL algorithm can get functions as arguments are the main reason for the power of the STL. Examples? For simplicity reason, I use in the following lines the container and not ranges.<\/p>\n<ul>\n<li><span style=\"font-family: courier new,courier;\">std::transform <\/span>modifies the elements of its container with the help of a function, which needs precisely one argument.<\/li>\n<li><span style=\"font-family: courier new,courier;\">std::remove_if<\/span> remove all elements from the container fulfilling the property. A predicate defines the condition. A predicate is a function that returns a boolean.<\/li>\n<li><span style=\"font-family: courier new,courier;\">std::accumulate<\/span> applies to all elements of the container a binary operation. The algorithm successively reduces the container to a final value.<\/li>\n<\/ul>\n<p>Let&#8217;s have a look at the three functions in action.<\/p>\n<p><!-- HTML generated using hilite.me --><\/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\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ algorithm.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;numeric&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;vector&gt;<\/span>\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  std::vector&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; myVec{1,2,3,4,5,6,7,8,9};\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"original:         \"<\/span>;\r\n  <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span> a: myVec) { std::cout &lt;&lt; a &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span>;}\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"\\n\\n\"<\/span>;\r\n  \r\n  std::transform(myVec.begin(),myVec.end(),myVec.begin(), [](<span style=\"color: #2b91af;\">int<\/span> i){ <span style=\"color: #0000ff;\">return<\/span> i*i; });\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::transform:   \"<\/span>;\r\n  <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span> a: myVec) { std::cout &lt;&lt; a &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span>;}\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"\\n\\n\"<\/span>;\r\n  \r\n  myVec={1,2,3,4,5,6,7,8,9};\r\n  \r\n  <span style=\"color: #0000ff;\">auto<\/span> logicalEnd= std::remove_if(myVec.begin(),myVec.end(), [](<span style=\"color: #2b91af;\">int<\/span> i){ <span style=\"color: #0000ff;\">return<\/span> !((i &lt; 3) or (i &gt; 8)); });\r\n  myVec.erase(logicalEnd,myVec.end());\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::remove_if:   \"<\/span>;\r\n  <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span> a: myVec) { std::cout &lt;&lt; a &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span>;}\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"\\n\\n\"<\/span>;\r\n  \r\n  myVec={1,2,3,4,5,6,7,8,9};\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"std::accumulate:  \"<\/span>;\r\n  <span style=\"color: #0000ff;\">auto<\/span> fak= std::accumulate(myVec.begin(),myVec.end(),1,[](<span style=\"color: #2b91af;\">int<\/span> fir, <span style=\"color: #2b91af;\">int<\/span> sec){ <span style=\"color: #0000ff;\">return<\/span> fir * sec; });\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"fak(10): \"<\/span> &lt;&lt; fak &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>I fully intentionally used lambda functions in the example. Although the algorithm can accept all, that behaves like a function. But lambda functions should be your first choice. C++ has supported lambda functions since C++11. That will also hold for the three additional features from C++11 that I used in the example: automatic type deduction with <span style=\"font-family: courier new,courier;\">auto<\/span> (lines 22, 32, and 39), unified initialization (lines 12, 26, and 36), and the range-based for-loop (line 22 and 32).<\/p>\n<p>The program should be self-explanatory in combination with the output. I will only mention one specialty of the STL algorithm in line 28. The <span style=\"font-family: courier new,courier;\">std::remove_if<\/span> does not remove &#8211; like all generic algorithms of the STL &#8211; the elements from the container. <span style=\"font-family: courier new,courier;\">std::remove_if<\/span> returns the logical end of the new container. This logical end is used by the <span style=\"font-family: courier new,courier;\">myVec.erase<\/span> method, which shortens the container to its correct length.<\/p>\n<p><strong>The method <span style=\"font-family: courier new,courier;\">erase<\/span> is not a generic algorithm of the STL but a method of <span style=\"font-family: courier new,courier;\">std::vector.<\/span>&nbsp;<\/strong><\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5125\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/algorithm.png\" alt=\"algorithm\" style=\"margin: 15px;\" width=\"433\" height=\"271\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/algorithm.png 433w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/algorithm-300x188.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/h3>\n<h2>What&#8217;s next?<\/h2>\n<p>I will write in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/functional-in-tr1-and-c-11\">next post<\/a> about Technical Report 1 from 2005. I&#8217;m particularly interested in the two functions <span style=\"font-family: courier new,courier;\">std::bind<\/span> and <span style=\"font-family: courier new,courier;\">std::function,<\/span> which introduce a new technique in C++: partial function application. Partial function application is similar to the functional technique currying, also called sch\u00f6nfinkeln. Sch\u00f6nfinkeln, that sounds extremely promising.<\/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<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ is not a functional programming language, but you can program in a functional style. What are the functional features of C++? I will answer this question for C++98.<\/p>\n","protected":false},"author":21,"featured_media":5120,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[365],"tags":[],"class_list":["post-5126","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-functional"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5126","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=5126"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5126\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5120"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}