{"id":2295,"date":"2025-02-15T22:21:32","date_gmt":"2025-02-15T22:21:32","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=2295"},"modified":"2025-04-04T19:23:39","modified_gmt":"2025-04-04T19:23:39","slug":"lambdas","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/lambdas\/","title":{"rendered":"Lambdas &amp; Closures"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Whats_the_Problem\"><\/span>What&#8217;s the Problem?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Remember the example we used, when we were talking about Function Objects in the Standard Template Library topic?<\/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>which outputs the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Result: 2310\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n\n\n\n<p>where we provided a class that acted as a function object because it defined an <strong>operator()<\/strong> method.<\/p>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Enter_Lambdas\"><\/span>Enter Lambdas<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p style=\"padding-bottom:var(--wp--preset--spacing--40)\">Sometimes it&#8217;s annoying to have to create an entire class to perform something that is only used once, and locally.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30)\">\n<p class=\"has-text-align-left\" style=\"font-size:clamp(15.747px, 0.984rem + ((1vw - 3.2px) * 0.645), 24px);\"><strong>Beware!<\/strong><\/p>\n\n\n\n<p>You are entering a C++ realm of strange, weird, arcane, and abbreviated syntax.<\/p>\n<\/blockquote>\n\n\n\n<p>So, we can do the following:<\/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\/\/  Lambdas\n\/\/\n\/\/  Created by Bryan Higgs on 2\/15\/2025.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;  \/\/ For vector\n#include &lt;numeric&gt;  \/\/ For accumulate()\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, \n                           [](int x, int y) \/\/ Our lambda\n                           {\n                             return x * y;\n                           }\n                          );\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\">\/\/  Lambdas<\/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 2\/15\/2025.<\/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\"><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\"><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: #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\">) \/\/ Our lambda<\/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: #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 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>which also outputs the same result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Result: 2310\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n\n\n\n<p>So, what did we do?<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>What is that really strange syntax? It&#8217;s a <strong><em>lambda expression<\/em><\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Lambda_Expressions\"><\/span>Lambda Expressions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Aside from the eleventh letter of the Greek alphabet ( <img loading=\"lazy\" decoding=\"async\" width=\"40\" height=\"27\" class=\"wp-image-2311\" style=\"width: 40px;\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2025\/02\/Lambda_uc_lc.svg.png\" alt=\"\" srcset=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2025\/02\/Lambda_uc_lc.svg.png 640w, https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2025\/02\/Lambda_uc_lc.svg-300x200.png 300w\" sizes=\"auto, (max-width: 40px) 100vw, 40px\" \/>), what is a <strong><em>Lambda<\/em><\/strong>?.<\/p>\n\n\n\n<p>A <strong>lambda expression<\/strong> (also called a <strong>lambda<\/strong> or <strong>closure<\/strong>) allows us to define an anonymous function inside another function. <\/p>\n\n\n\n<p>The nesting is important, as it allows us both to avoid namespace naming pollution, and to define the function as close to where it is used as possible.<\/p>\n\n\n\n<p>The syntax of a Lambda Expression is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>&#91; <em>capture-clause<\/em> ] ( <em>parameter-lis<\/em>t ) -&gt; <em>return-type<\/em> { <em>statements<\/em>; }<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <em><strong>capture-clause<\/strong><\/em> can be empty if no captures are needed.<\/li>\n\n\n\n<li>The <em><strong>parameter-list<\/strong><\/em> can be empty if no parameters are required. It can also be omitted entirely unless a return type is specified.<\/li>\n\n\n\n<li>The <em><strong>return-type<\/strong><\/em> is optional, and if omitted, <strong>auto<\/strong> will be assumed (type deduction is used to determine the return type).<\/li>\n\n\n\n<li>lambdas have no name &#8211; they are anonymous, just like numeric literals, etc.<\/li>\n<\/ul>\n\n\n\n<p>The most trivial lambda expression is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>&#91;] {}; \/\/ a lambda with an omitted return type, no captures, and omitted parameters.<\/strong><\/code><\/pre>\n\n\n\n<p>It doesn&#8217;t do anything.<\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size\" style=\"font-size:clamp(15.747px, 0.984rem + ((1vw - 3.2px) * 0.645), 24px);\"><strong>auto in C++<\/strong><\/p>\n\n\n\n<p>In C++, the <strong>auto<\/strong> keyword instructs the compiler to deduce the type of a variable from its initialization expression. Introduced in C++11, it simplifies code, especially when dealing with complex types, by eliminating the need to explicitly declare the type.<\/p>\n\n\n\n<p>When <strong>auto<\/strong> is used, the compiler infers the variable&#8217;s type based on the expression used to initialize it. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>auto x = 10; \/\/ x is deduced as int\nauto y = 3.14; \/\/ y is deduced as double\nauto z = \"hello\"; \/\/ z is deduced as const char*<\/strong><\/code><\/pre>\n\n\n\n<p><strong>auto<\/strong> is particularly useful in situations involving templates, lambda expressions, and complex return types, where explicitly specifying the type can be verbose or cumbersome. It enhances code readability and reduces the risk of type-related errors.<\/p>\n\n\n\n<p>However, <strong>auto<\/strong> should be used judiciously. Overuse can sometimes reduce code clarity, especially when the deduced type is not immediately obvious. It&#8217;s generally recommended to use <strong>auto<\/strong> when the type is clear from the initialization or when dealing with complex types.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Storing_a_Lambda_Expression_in_a_Variable\"><\/span>Storing a Lambda Expression in a Variable<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In the above example, we defined the lambda where it was needed. This use of a lambda is sometimes called a <strong><em>function literal<\/em><\/strong>.<\/p>\n\n\n\n<p>But we can store the lambda expression in a variable:<\/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\/\/  Lambdas\n\/\/\n\/\/  Created by Bryan Higgs on 2\/15\/2025.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;  \/\/ For vector\n#include &lt;numeric&gt;  \/\/ For accumulate()\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  auto multiply\n  {\n    [](int x, int y)\n    {\n      return x * y;\n    }\n  };\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\">\/\/  Lambdas<\/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 2\/15\/2025.<\/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\"><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 cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">auto<\/span><span style=\"color: #000000\"> 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: #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>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    {<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><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 cbp-line-highlight\"><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\">, multiply);<\/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>which also outputs the same result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Result: 2310\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n\n\n\n<p>Much more readable, don&#8217;t you think?<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>But didn&#8217;t we invent lambdas to make them local, and nameless?<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Passing_a_Lambda_Expression_to_a_Function\"><\/span>Passing a Lambda Expression to a Function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>There are four ways of passing a lambda expression to a function:<\/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\/\/  Lambdas\n\/\/\n\/\/  Created by Bryan Higgs on 2\/15\/2025.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;functional&gt;\n\n\/\/ Case 1: use a `std::function` parameter\nvoid repeat1(int repetitions, const std::function&lt;void(int)&gt;&amp; fn)\n{\n  for (int i{ 0 }; i &lt; repetitions; ++i)\n    fn(i);\n}\n\n\/\/ Case 2: use a function template with a type template parameter\ntemplate &lt;typename T&gt;\nvoid repeat2(int repetitions, const T&amp; fn)\n{\n  for (int i{ 0 }; i &lt; repetitions; ++i)\n    fn(i);\n}\n\n\/\/ Case 3: use the abbreviated function template syntax (C++20)\nvoid repeat3(int repetitions, const auto&amp; fn)\n{\n  for (int i{ 0 }; i &lt; repetitions; ++i)\n    fn(i);\n}\n\n\/\/ Case 4: use function pointer (only for lambda with no captures)\nvoid repeat4(int repetitions, void (*fn)(int))\n{\n  for (int i{ 0 }; i &lt; repetitions; ++i)\n    fn(i);\n}\n\nint main()\n{\n  auto lambda = [](int i)\n  {\n    std::cout &lt;&lt; i &lt;&lt; std::endl;\n  };\n\n  std::cout &lt;&lt; &quot;repeat1 :&quot; &lt;&lt; std::endl;\n  repeat1(3, lambda);\n  std::cout &lt;&lt; &quot;repeat2 :&quot; &lt;&lt; std::endl;\n  repeat2(3, lambda);\n  std::cout &lt;&lt; &quot;repeat3 :&quot; &lt;&lt; std::endl;\n  repeat3(3, lambda);\n  std::cout &lt;&lt; &quot;repeat4 :&quot; &lt;&lt; std::endl;\n  repeat4(3, lambda);\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\">\/\/  Lambdas<\/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 2\/15\/2025.<\/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;functional&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">\/\/ Case 1: use a `std::function` parameter<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">repeat1<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">repetitions<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">function<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">)&gt;<\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">fn<\/span><span style=\"color: #000000\">)<\/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: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i{ <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\"> }; i &lt; repetitions; ++i)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">fn<\/span><span style=\"color: #000000\">(i);<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">\/\/ Case 2: use a function template with a type template parameter<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">template<\/span><span style=\"color: #000000\"> &lt;<\/span><span style=\"color: #0000FF\">typename<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">T<\/span><span style=\"color: #000000\">&gt;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">repeat2<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">repetitions<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">T<\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">fn<\/span><span style=\"color: #000000\">)<\/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: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i{ <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\"> }; i &lt; repetitions; ++i)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">fn<\/span><span style=\"color: #000000\">(i);<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">\/\/ Case 3: use the abbreviated function template syntax (C++20)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">repeat3<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">repetitions<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">auto&amp;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">fn<\/span><span style=\"color: #000000\">)<\/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: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i{ <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\"> }; i &lt; repetitions; ++i)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">fn<\/span><span style=\"color: #000000\">(i);<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">\/\/ Case 4: use function pointer (only for lambda with no captures)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">repeat4<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">repetitions<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> (*<\/span><span style=\"color: #001080\">fn<\/span><span style=\"color: #000000\">)(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">))<\/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: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i{ <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\"> }; i &lt; repetitions; ++i)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">fn<\/span><span style=\"color: #000000\">(i);<\/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>\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\">auto<\/span><span style=\"color: #000000\"> lambda = [](<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)<\/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: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; i &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/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: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;repeat1 :&quot;<\/span><span style=\"color: #000000\"> &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 style=\"color: #795E26\">repeat1<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, lambda);<\/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;repeat2 :&quot;<\/span><span style=\"color: #000000\"> &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 style=\"color: #795E26\">repeat2<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, lambda);<\/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;repeat3 :&quot;<\/span><span style=\"color: #000000\"> &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 style=\"color: #795E26\">repeat3<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, lambda);<\/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;repeat4 :&quot;<\/span><span style=\"color: #000000\"> &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 style=\"color: #795E26\">repeat4<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3<\/span><span style=\"color: #000000\">, lambda);<\/span><\/span>\n<span class=\"line\"><\/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<ol class=\"wp-block-list\">\n<li>Using a <strong>std::function<\/strong> parameter<br><br>Here, we can see what the parameters and return type of the <strong>std::function<\/strong> are, but adds some overhead because of implicit conversion of the lambda.<br><\/li>\n\n\n\n<li style=\"padding-top:var(--wp--preset--spacing--30)\">Using a function template with a type parameter<br><br>Here, we use a template type parameter, T. It involves a function instantiation where T matches the actual type of the lambda. More efficient than case 1., but the parameters and return type of T are not clear.<br><\/li>\n\n\n\n<li style=\"padding-top:var(--wp--preset--spacing--30)\">Using the abbreviated function template syntax<br><br>Here, the compiler generates a function template identical to case 2.<br><\/li>\n\n\n\n<li style=\"padding-top:var(--wp--preset--spacing--30)\">Using a function pointer (only for lambda with no captures)<br><br>Here, since a lambda with no captures implicitly converts to a function pointer, and so we can pass a lambda<\/li>\n<\/ol>\n\n\n\n<p style=\"padding-top:var(--wp--preset--spacing--30)\">This program outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>repeat1 :\n0\n1\n2\nrepeat2 :\n0\n1\n2\nrepeat3 :\n0\n1\n2\nrepeat4 :\n0\n1\n2\nProgram ended with exit code: 0<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Lambda_Captures\"><\/span>Lambda Captures<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let&#8217;s look at another example. <\/p>\n\n\n\n<p>Remember how we used the STL <strong>find()<\/strong> algorithm with a <strong>std::vector<\/strong>?<\/p>\n\n\n\n<p>Here&#8217;s a variation of that. It uses <strong>find_if()<\/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\" style=\"flex-basis:75%\">\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);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\/\/  Lambdas\n\/\/\n\/\/  Created by Bryan Higgs on 2\/15\/2025.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;vector&gt;  \/\/ For vector\n#include &lt;algorithm&gt;  \/\/ For find()\n#include &lt;string_view&gt;\n\nusing namespace std;\n\nint main(int argc, const char * argv[])\n{\n  vector&lt;string_view&gt; vector{ &quot;owl&quot;, &quot;thrush&quot;, &quot;woodpecker&quot;, &quot;cardinal&quot;, &quot;nuthatch&quot;};\n  auto found{ find_if( vector.begin(), vector.end(),\n                       [](std::string_view str)\n                       {\n                         return str.find(&quot;nut&quot;) != std::string_view::npos;\n                       }\n                     ) };\n  \n  if ( found == vector.end() )\n  {\n    cout &lt;&lt; &quot;Not found&quot; &lt;&lt; endl;\n  }\n  else\n  {\n    cout &lt;&lt; &quot;Found &quot; &lt;&lt; *found &lt;&lt; endl;\n  }\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\">\/\/  Lambdas<\/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 2\/15\/2025.<\/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;algorithm&gt;<\/span><span style=\"color: #0000FF\">  <\/span><span style=\"color: #008000\">\/\/ For find()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;string_view&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">using<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">namespace<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><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\">  vector&lt;string_view&gt; vector{ <\/span><span style=\"color: #A31515\">&quot;owl&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;thrush&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;woodpecker&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;cardinal&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;nuthatch&quot;<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">auto<\/span><span style=\"color: #000000\"> found{ <\/span><span style=\"color: #795E26\">find_if<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/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\">::<\/span><span style=\"color: #267F99\">string_view<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">str<\/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: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">str<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">find<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;nut&quot;<\/span><span style=\"color: #000000\">) != <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string_view<\/span><span style=\"color: #000000\">::npos;<\/span><\/span>\n<span class=\"line\"><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>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> ( found == <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/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\">    cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Not found&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; 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\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Found &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *found &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><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: #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\" style=\"flex-basis:15%\">\n<p>which outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Found nuthatch\nProgram ended with exit code: 0<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p style=\"font-size:clamp(15.747px, 0.984rem + ((1vw - 3.2px) * 0.645), 24px);\"><strong>Explanation<\/strong><\/p>\n\n\n\n<p>The above code may need some explanation:<\/p>\n\n\n\n<p><strong>std::string_view<\/strong> provides read-only access to an <em>existing<\/em> string (a C-style string, a <strong>std::string<\/strong>, or another <strong>std::string_view<\/strong>) without making a copy. It is used to eliminate unnecessary string copies, which tend to be expensive.<\/p>\n\n\n\n<p><strong>find_if()<\/strong> Returns an iterator to the first element in the range that matches the third parameter passed (in this case, our lambda expression). If no such element is found, the function returns <var>last<\/var>.<\/p>\n\n\n\n<p><strong>found()<\/strong> is an iterator initialized from the call to <strong>find_if()<\/strong>. <br><strong>auto<\/strong> is a convenient way for us to ask the compiler to supply the type.<\/p>\n\n\n\n<p><strong>std::string_view::npos<\/strong> is a static member constant of the <strong>std::string_view<\/strong> class in C++. It represents the maximum possible value for the <strong>size_t<\/strong> type, typically used to indicate the absence of a valid position within the string view. It is often used as a return value by member functions like <strong>find<\/strong>, <strong>rfind<\/strong>, etc., to signal that the search or operation failed to find a match.<\/p>\n<\/blockquote>\n\n\n\n<p>Now, let&#8217;s generalize the program, to ask the user what they would like to search for:<\/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\" style=\"flex-basis:75%\">\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\/\/  Lambdas\n\/\/\n\/\/  Created by Bryan Higgs on 2\/15\/2025.\n\/\/\n\n#include &lt;algorithm&gt;\n#include &lt;array&gt;\n#include &lt;iostream&gt;\n#include &lt;string_view&gt;\n#include &lt;string&gt;\n\nusing namespace std;\n\nint main(int argc, const char * argv[])\n{\n  vector&lt;string_view&gt; vector{ &quot;owl&quot;, &quot;thrush&quot;, &quot;woodpecker&quot;, &quot;cardinal&quot;, &quot;nuthatch&quot;};\n\n  \/\/ Ask the user what to search for.\n  std::cout &lt;&lt; &quot;What would you like to search for? &quot;;\n\n  std::string search{}; \/\/ Storage for user input\n  std::cin &gt;&gt; search;   \/\/ Obtain user's string\n\n  auto found{ find_if( vector.begin(), vector.end(), \n                        [](std::string_view str) \/\/ lambda\n                        {\n                          \/\/ Search for user's specified string.\n                          return str.find(search) != std::string_view::npos;\n                             \/\/ Variable 'search' cannot be implicitly captured\n                             \/\/ in a lambda with no capture-default specified\n\n                        }\n                     )\n            };\n\n  if (found == vector.end())\n  {\n    cout &lt;&lt; &quot;Not found&quot; &lt;&lt; endl;\n  }\n  else\n  {\n    cout &lt;&lt; &quot;Found &quot; &lt;&lt; *found &lt;&lt; endl;\n  }\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\">\/\/  Lambdas<\/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 2\/15\/2025.<\/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;algorithm&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;array&gt;<\/span><\/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;string_view&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;string&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">using<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">namespace<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><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\">  vector&lt;string_view&gt; vector{ <\/span><span style=\"color: #A31515\">&quot;owl&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;thrush&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;woodpecker&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;cardinal&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;nuthatch&quot;<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">  \/\/ Ask the user what to search for.<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;What would you like to search for? &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::string search{};<\/span><span style=\"color: #008000\"> \/\/ Storage for user input<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cin &gt;&gt; search;<\/span><span style=\"color: #008000\">   \/\/ Obtain user&#39;s string<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">auto<\/span><span style=\"color: #000000\"> found{ <\/span><span style=\"color: #795E26\">find_if<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/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: #267F99\">string_view<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">str<\/span><span style=\"color: #000000\">) \/\/ lambda<\/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: #008000\">                          \/\/ Search for user&#39;s specified string.<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                          <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">str<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">find<\/span><span style=\"color: #000000\">(search) != <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string_view<\/span><span style=\"color: #000000\">::npos;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">                             \/\/ Variable &#39;search&#39; cannot be implicitly captured<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">                             \/\/ in a lambda with no capture-default specified<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><\/span>\n<span class=\"line cbp-line-highlight\"><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>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (found == <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/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\">    cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Not found&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; 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\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Found &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *found &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><\/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\" style=\"flex-basis:15%\"><\/div>\n<\/div>\n\n\n\n<p>But we get a compile-time error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Variable 'search' cannot be implicitly captured in a lambda with no capture-default specified<\/strong>\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Whats_the_problem\"><\/span>What&#8217;s the problem?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Unlike nested blocks, where any identifier accessible in the outer block is accessible in the nested block, lambdas can only access certain kinds of objects that have been defined outside the lambda. This includes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Objects with static (or thread local) storage duration (including global variables and static locals)<\/li>\n\n\n\n<li>Objects that are <strong>constexpr<\/strong> (explicitly or implicitly)<\/li>\n<\/ul>\n\n\n\n<p>Since <strong>search<\/strong> fulfills none of these requirements, the lambda can\u2019t see it.<\/p>\n\n\n\n<p>We can fix this situation by specifying the necessary variable(s) in the lambda&#8217;s <strong>capture-clause<\/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\" style=\"flex-basis:75%\">\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\/\/  Lambdas\n\/\/\n\/\/  Created by Bryan Higgs on 2\/15\/2025.\n\/\/\n\n#include &lt;algorithm&gt;\n#include &lt;array&gt;\n#include &lt;iostream&gt;\n#include &lt;string_view&gt;\n#include &lt;string&gt;\n\nusing namespace std;\n\nint main(int argc, const char * argv[])\n{\n  vector&lt;string_view&gt; vector{ &quot;owl&quot;, &quot;thrush&quot;, &quot;woodpecker&quot;, &quot;cardinal&quot;, &quot;nuthatch&quot;};\n\n  \/\/ Ask the user what to search for.\n  std::cout &lt;&lt; &quot;What would you like to search for? &quot;;\n\n  std::string search{}; \/\/ Storage for user input\n  std::cin &gt;&gt; search;   \/\/ Obtain user's string\n\n  auto found{ find_if( vector.begin(), vector.end(), \n                        [search](std::string_view str) \/\/ lambda\n                        {\n                          \/\/ Search for user's specified string.\n                          return str.find(search) != std::string_view::npos;\n                        }\n                     )\n            };\n\n  if (found == vector.end())\n  {\n    cout &lt;&lt; &quot;Not found&quot; &lt;&lt; endl;\n  }\n  else\n  {\n    cout &lt;&lt; &quot;Found &quot; &lt;&lt; *found &lt;&lt; endl;\n  }\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\">\/\/  Lambdas<\/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 2\/15\/2025.<\/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;algorithm&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;array&gt;<\/span><\/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;string_view&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;string&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">using<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">namespace<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">std<\/span><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\">  vector&lt;string_view&gt; vector{ <\/span><span style=\"color: #A31515\">&quot;owl&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;thrush&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;woodpecker&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;cardinal&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;nuthatch&quot;<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Ask the user what to search for.<\/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;What would you like to search for? &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::string search{};<\/span><span style=\"color: #008000\"> \/\/ Storage for user input<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cin &gt;&gt; search;<\/span><span style=\"color: #008000\">   \/\/ Obtain user&#39;s string<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">auto<\/span><span style=\"color: #000000\"> found{ <\/span><span style=\"color: #795E26\">find_if<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">begin<\/span><span style=\"color: #000000\">(), <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/span><span style=\"color: #000000\">(), <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                        [<\/span><span style=\"color: #001080\">search<\/span><span style=\"color: #000000\">](<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string_view<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">str<\/span><span style=\"color: #000000\">) \/\/ lambda<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">                          \/\/ Search for user&#39;s specified string.<\/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: #001080\">str<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">find<\/span><span style=\"color: #000000\">(search) != <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string_view<\/span><span style=\"color: #000000\">::npos;<\/span><\/span>\n<span class=\"line\"><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>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (found == <\/span><span style=\"color: #001080\">vector<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">end<\/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\">    cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Not found&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; 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\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Found &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; *found &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><\/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\" style=\"flex-basis:15%\"><\/div>\n<\/div>\n\n\n\n<p>Here&#8217;s what the program outputs when run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>What would you like to search for? <\/strong><em>pecker<\/em><strong>\nFound woodpecker\nProgram ended with exit code: 0<\/strong><\/code><\/pre>\n\n\n\n<p>(the text in italics is what the user entered)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Lambdas_vs_Closures\"><\/span>Lambdas vs Closures<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The difference between a lambda expression and a closure is sometimes confusing, since both are often discussed together.<\/p>\n\n\n\n<p>Scott Meyers has described them as follows:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The term &#8220;lambda&#8221; is short for <em>lambda expression<\/em>, and a lambda is just that: an <em>expression<\/em>. As such, it exists only in a program&#8217;s source code. A lambda does not exist at runtime.<br><br>The runtime effect of a lambda expression is the generation of an object. Such objects are known as <em>closures<\/em>.<br><br>Given<br><br><code>\u00a0\u00a0auto f = [&amp;](int x, int y) { return fudgeFactor * (x + y); };<br><\/code><br>the expression to the right of the &#8220;=&#8221; is the lambda expression (i.e., &#8220;the lambda&#8221;), and the runtime object created by that expression is the closure.<br><br>You could be forgiven for thinking that, in this example, <code>f<\/code> was the closure, but it&#8217;s not. <code>f<\/code> is a <em>copy<\/em> of the closure. The process of copying the closure into <code>f<\/code> may be optimized into a move (whether it is depends on the types captured by the lambda), but that doesn&#8217;t change the fact that <code>f<\/code> itself is not the closure. The actual closure object is a temporary that&#8217;s typically destroyed at the end of the statement.<br><br><strong>The distinction between a lambda and the corresponding closure is precisely equivalent to the distinction between a class and an instance of the class.<\/strong> A class exists only in source code; it doesn&#8217;t exist at runtime. What exists at runtime are objects of the class type.\u00a0 Closures are to lambdas as objects are to classes. This should not be a surprise, because each lambda expression causes a unique class to be generated (during compilation) and also causes an object of that class type&#8211;a closure&#8211;to be created (at runtime).<\/p>\n<cite>&#8211; Scott Meyers <a href=\"https:\/\/scottmeyers.blogspot.com\/2013\/05\/lambdas-vs-closures.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/scottmeyers.blogspot.com\/2013\/05\/lambdas-vs-closures.html<\/a><\/cite><\/blockquote>\n\n\n\n<p>For example:<\/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\" style=\"flex-basis:60%\">\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\/\/  Closures\n\/\/\n\/\/  Created by Bryan Higgs on 4\/4\/25.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;functional&gt;\n\nstd::function&lt;void(void)&gt; closureWrapper1()\n{\n  int x = 10;\n  return [x]()\n  {\n    std::cout &lt;&lt; &quot;Value in the closure: &quot; &lt;&lt; x &lt;&lt; std::endl;\n  };\n}\n\nstd::function&lt;void(void)&gt; closureWrapper2()\n{\n  int x = 10;\n  return [&amp;x]()\n  {\n    x += 1;\n    std::cout &lt;&lt; &quot;Value in the closure: &quot; &lt;&lt; x &lt;&lt; std::endl;\n  };\n}\n\nint main(int argc, const char * argv[])\n{\n  int x = 10;\n  auto func0 = [&amp;x]()\n  {\n    x += 1;\n    std::cout &lt;&lt; &quot;Value in the closure: &quot; &lt;&lt; x &lt;&lt; std::endl;\n  };\n  \n  std::function&lt;void(void)&gt; func1 = closureWrapper1();\n  std::function&lt;void(void)&gt; func2 = closureWrapper2();\n  \n  func0();\n  func0();\n  func0();\n  std::cout &lt;&lt; &quot;-------------------------&quot; &lt;&lt; std::endl;\n  func1();\n  func1();\n  func1();\n  std::cout &lt;&lt; &quot;-------------------------&quot; &lt;&lt; std::endl;\n  func2();\n  func2();\n  func2();\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\">\/\/  Closures<\/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 4\/4\/25.<\/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;functional&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">function<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">)&gt; <\/span><span style=\"color: #795E26\">closureWrapper1<\/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\"> x = <\/span><span style=\"color: #098658\">10<\/span><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: #001080\">x<\/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: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Value in the closure: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; x &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>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">function<\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">)&gt; <\/span><span style=\"color: #795E26\">closureWrapper2<\/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\"> x = <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> [&amp;<\/span><span style=\"color: #001080\">x<\/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\">    x += <\/span><span style=\"color: #098658\">1<\/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;Value in the closure: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; x &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>\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\"> x = <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">auto<\/span><span style=\"color: #000000\"> func0 = [&amp;<\/span><span style=\"color: #001080\">x<\/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\">    x += <\/span><span style=\"color: #098658\">1<\/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;Value in the closure: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; x &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>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::function&lt;<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">)&gt; func1 = <\/span><span style=\"color: #795E26\">closureWrapper1<\/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\">::function&lt;<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">)&gt; func2 = <\/span><span style=\"color: #795E26\">closureWrapper2<\/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: #795E26\">func0<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">func0<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">func0<\/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;-------------------------&quot;<\/span><span style=\"color: #000000\"> &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 style=\"color: #795E26\">func1<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">func1<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">func1<\/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;-------------------------&quot;<\/span><span style=\"color: #000000\"> &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 style=\"color: #795E26\">func2<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">func2<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">func2<\/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: #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\" style=\"flex-basis:40%\">\n<p>which outputs (at least in my compiler run time):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Value in the closure: 11\nValue in the closure: 12\nValue in the closure: 13\n-------------------------\nValue in the closure: 10\nValue in the closure: 10\nValue in the closure: 10\n-------------------------\nValue in the closure: 2\nValue in the closure: 3\nValue in the closure: 4\nProgram ended with exit code: 0\n<\/code><\/pre>\n\n\n\n<p><strong>func0<\/strong> is a closure; it captures the reference to the variable x in the scope of main. Consequently, every time func0 is called, the value of x gets increased by 1.<\/p>\n\n\n\n<p><strong>func1<\/strong> and <strong>func2<\/strong> are not closures; they are std::function wrapper objects that wrap closures.<\/p>\n\n\n\n<p><strong>func1<\/strong> captures the value of the variable x in the scope of closureWrapper1 by making a copy of it. Consequently, every time func1 is called, the value of the closure is always 10. After returning from the ordinary function, the local variables in the that function will be out of scope.<\/p>\n\n\n\n<p><strong>func2<\/strong> captures the reference to the variable x in the scope of closureWrapper2. The reference remembers the address of x. However, after the function returns, the local variable x in the ordinary function will be out of scope. As a result, the value will be undefined.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>As Scott Meyers says:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>I noted above that a closure is typically destroyed at the end of the statement in which it is created.\u00a0 The exception to this rule is when you bind the closure to a reference. The simplest way to do that is to employ a <em>universal reference<\/em>,<br><br><code>\u00a0\u00a0auto&amp;&amp; rrefToClosure = [&amp;](int x, int y) { return fudgeFactor * (x + y); };<br><\/code><br>but binding it to an lvalue-reference-to-<code>const<\/code> will also work:<br><br><code>\u00a0\u00a0const auto&amp; lrefToConstToClosure = [&amp;](int x, int y) { return fudgeFactor * (x + y); };<\/code><\/p>\n<cite>&#8211; Scott Meyers <a href=\"https:\/\/scottmeyers.blogspot.com\/2013\/05\/lambdas-vs-closures.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/scottmeyers.blogspot.com\/2013\/05\/lambdas-vs-closures.html<\/a><\/cite><\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What&#8217;s the Problem? Remember the example we used, when we were talking about Function Objects in the Standard Template Library topic? which outputs the following: where we provided a class that acted as a function object because it defined an operator() method. Enter Lambdas Sometimes it&#8217;s annoying to have to create an entire class to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":910,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2295","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2295","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=2295"}],"version-history":[{"count":26,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2295\/revisions"}],"predecessor-version":[{"id":2350,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/2295\/revisions\/2350"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/910"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=2295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}