{"id":2183,"date":"2025-01-28T20:02:49","date_gmt":"2025-01-28T20:02:49","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=2183"},"modified":"2025-01-28T21:39:07","modified_gmt":"2025-01-28T21:39:07","slug":"function-objects","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/the-standard-template-library-2\/function-objects\/","title":{"rendered":"Function Objects"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Function Objects<\/h2>\n\n\n\n<p>In the previous example, we are passing the address of a function (<strong>mult()<\/strong>) into the <strong>accumulate()<\/strong> function.<\/p>\n\n\n\n<p>We can instead use a <em><strong>Function Object<\/strong><\/em>:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  main.cpp\n\/\/  STL Function Objects\n\/\/\n\/\/  Created by Bryan Higgs on 10\/23\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;  \/\/ For vector\n#include &lt;numeric&gt;  \/\/ For accumulate()\n\nclass Multiply\n{\npublic:\n  int operator()(int x, int y) const { return x * y; }\n};\n\nint main(int argc, const char * argv[])\n{\n  int values[] = {2, 3, 5, 7, 11};\n  size_t len = sizeof(values)\/sizeof(values[0]);\n  \n  \/\/ Initialize vector using pointer iterator\n  std::vector&lt;int&gt; v(&amp;values[0], &amp;values[len]);\n  \n  int product = accumulate(v.begin(), v.end(), 1, Multiply());\n  std::cout &lt;&lt; &quot;Result: &quot; &lt;&lt; product &lt;&lt; std::endl;\n  \n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Function Objects<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 10\/23\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;vector&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For vector<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;numeric&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For accumulate()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Multiply<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #AF00DB\">operator()<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">y<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> { <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> x * y; }<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> values[] = {<\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">7<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">11<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">size_t<\/span><span style=\"color: #000000\"> len = <\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(values)\/<\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Initialize vector using pointer iterator<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::vector&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt; <\/span><span style=\"color: #795E26\">v<\/span><span style=\"color: #000000\">(&amp;<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">], &amp;<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[len]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> product = <\/span><span style=\"color: #795E26\">accumulate<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #795E26\">Multiply<\/span><span style=\"color: #000000\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; product &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>&#8230; which produces the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>Result: 2310\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>A <em>function object<\/em>, or <em>functor<\/em>, is&nbsp;<strong>any type that implements operator()<\/strong>. This operator is referred to as the <em>call operator<\/em> or sometimes the <em>application operator<\/em>. The&nbsp;C++ Standard Library&nbsp;uses function objects primarily as sorting criteria for containers and in algorithms.<\/p>\n\n\n\n<p>Reasons for using a function object instead of an ordinary function include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A function object can carry more potentially useful context than can an ordinary function.<\/li>\n\n\n\n<li>A function object&#8217;s <strong>operator()<\/strong> can be inlined, whereas passing the address of an ordinary function precludes inlining.<\/li>\n<\/ul>\n\n\n\n<p>STL provides the following function objects:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes dark-header\"><table class=\"has-palette-color-8-background-color has-background\" style=\"border-width:2px\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">Arithmetic<\/th><th class=\"has-text-align-center\" data-align=\"center\">Relational<\/th><th class=\"has-text-align-center\" data-align=\"center\">Logical<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><code>plus<\/code>&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">equal_to&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">logical_and&lt;T&gt;<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">minus&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">not_equal_to&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">logical_or&lt;T&gt;<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">multiplies&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">less&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">logical_not&lt;T&gt;<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">divides&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">greater&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\"><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">modulus&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">less_equal&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\"><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">negate&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\">greater_equal&lt;T&gt;<\/td><td class=\"has-text-align-center\" data-align=\"center\"><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So, we can modify our code to make use of the <em>STL-provided<\/em> function object, <strong>multiplies&lt;T&gt;<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  main.cpp\n\/\/  STL Function Objects\n\/\/\n\/\/  Created by Bryan Higgs on 10\/23\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;  \/\/ For vector\n#include &lt;numeric&gt;  \/\/ For accumulate()\n#include &lt;functional&gt;   \/\/ For multiplies\n\nint main(int argc, const char * argv[])\n{\n  int values[] = {2, 3, 5, 7, 11};\n  size_t len = sizeof(values)\/sizeof(values[0]);\n  \n  \/\/ Initialize vector using pointer iterator\n  std::vector&lt;int&gt; v(&amp;values[0], &amp;values[len]);\n  int product = accumulate(v.begin(), v.end(), 1, \n                           std::multiplies&lt;int&gt;() );\n  std::cout &lt;&lt; &quot;Result: &quot; &lt;&lt; product &lt;&lt; std::endl;\n  \n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  STL Function Objects<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 10\/23\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;vector&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For vector<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;numeric&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For accumulate()<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;functional&gt;<\/span><span style=\"color: #0000FF\">   <\/span><span style=\"color: #008000\">\/\/ For multiplies<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> values[] = {<\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">7<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">11<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">size_t<\/span><span style=\"color: #000000\"> len = <\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(values)\/<\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Initialize vector using pointer iterator<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::vector&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt; <\/span><span style=\"color: #795E26\">v<\/span><span style=\"color: #000000\">(&amp;<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">], &amp;<\/span><span style=\"color: #001080\">values<\/span><span style=\"color: #000000\">[len]);<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> product = <\/span><span style=\"color: #795E26\">accumulate<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">v<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                           <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">multiplies<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">&gt;() );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Result: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; product &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>&#8230; which produces the same output as before.<\/p>\n<\/div>\n<\/div>\n\n\n\n<div style=\"height:47px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size has-medium-font-size\"><a class=\"wp-block-button__link has-palette-color-8-color has-text-color has-link-color wp-element-button\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/the-standard-template-library-2\/adapters\/\">Next: Adapters<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Function Objects In the previous example, we are passing the address of a function (mult()) into the accumulate() function. We can instead use a Function Object: &#8230; which produces the following: A function object, or functor, is&nbsp;any type that implements operator(). This operator is referred to as the call operator or sometimes the application operator. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2150,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2183","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=2183"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2183\/revisions"}],"predecessor-version":[{"id":2224,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2183\/revisions\/2224"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2150"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=2183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}