<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lesley Lai</title>
    <description>The latest articles on DEV Community by Lesley Lai (@lesleylai).</description>
    <link>https://dev.to/lesleylai</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F287445%2Fa5faf51c-bcd3-4ea6-a713-b294aa1c4c95.jpg</url>
      <title>DEV Community: Lesley Lai</title>
      <link>https://dev.to/lesleylai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lesleylai"/>
    <language>en</language>
    <item>
      <title>What is std::function in C++, and why we need them?</title>
      <dc:creator>Lesley Lai</dc:creator>
      <pubDate>Tue, 19 Jan 2021 11:31:39 +0000</pubDate>
      <link>https://dev.to/lesleylai/what-is-std-function-in-c-and-why-we-need-them-48d0</link>
      <guid>https://dev.to/lesleylai/what-is-std-function-in-c-and-why-we-need-them-48d0</guid>
      <description>&lt;p&gt;Yesterday, someone in the &lt;a href="https://www.includecpp.org/discord/"&gt;#include&lt;/a&gt; discord server asked the following question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;how &lt;code&gt;std::function&lt;/code&gt; works with lambda captures and functions handling I still don't understand&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below was my answer to the question, with some typo-fixes and expansions:&lt;/p&gt;

&lt;h2&gt;
  
  
  Invocables can have different types even if their parameter and return type are the same
&lt;/h2&gt;

&lt;p&gt;Lambda expressions can be considered syntactic sugar over classes with &lt;code&gt;operator()&lt;/code&gt; defined. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;lambda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is roughly equivalent to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;__Lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="p"&gt;()(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;lambda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;__Lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One consequence is that every lambda expression has a distinct type. For example, in the below snippet,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;lambda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;lambda2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;lambda&lt;/code&gt; and &lt;code&gt;lambda2&lt;/code&gt; have different types, even though they both take an &lt;code&gt;int&lt;/code&gt; and return an &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;C++ also have functions, which are distinct from classes with &lt;code&gt;operator()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The motivation for &lt;code&gt;std::function&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Then, how do we store such an invocable object that takes an int and returns an int disregard of its types?&lt;/p&gt;

&lt;p&gt;We need &lt;code&gt;std::function&lt;/code&gt; to accomplish such a task. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A canonical use case for storing an invocable in this fashion is a task system, where you probably want to store callbacks in a container to execute later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;TaskQueue&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;mutex&lt;/span&gt; &lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;condition_variable&lt;/span&gt; &lt;span class="n"&gt;ready&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// member functions&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Type Erasure
&lt;/h2&gt;

&lt;p&gt;To make &lt;code&gt;func&lt;/code&gt; accepts both &lt;code&gt;lambda&lt;/code&gt; and &lt;code&gt;lambda2&lt;/code&gt;,&lt;br&gt;
&lt;code&gt;std::function&lt;/code&gt; needs to have constructors that take any function object or plain function that satisfies its signature.&lt;br&gt;
And we need to perform &lt;em&gt;type erasure&lt;/em&gt; to achieve this behavior.&lt;/p&gt;

&lt;p&gt;There are various techniques to implement type erasure in C++,&lt;br&gt;
and it is not a topic I can fit into this post. But the high-level idea is that &lt;code&gt;std::function&lt;/code&gt; needs some function pointer that can invoke the lambda and some storage space to store lambda captures.&lt;br&gt;
The data need to be allocated on the heap since lambda expressions (or invocable classes) can have arbitrary sized capture. However, all major &lt;code&gt;std::function&lt;/code&gt; implementations also perform &lt;em&gt;small buffer optimization&lt;/em&gt; if your lambda is small enough to fit into a predefined capacity. In that case, all data can be allocated directly inside the &lt;code&gt;std::function&lt;/code&gt; object itself, and no additional heap allocation is performed.&lt;/p&gt;

</description>
      <category>cpp</category>
    </item>
    <item>
      <title>Resources that help you to delve into C++</title>
      <dc:creator>Lesley Lai</dc:creator>
      <pubDate>Sat, 16 Jan 2021 18:24:06 +0000</pubDate>
      <link>https://dev.to/lesleylai/resources-that-help-you-to-delve-into-c-2ngb</link>
      <guid>https://dev.to/lesleylai/resources-that-help-you-to-delve-into-c-2ngb</guid>
      <description>&lt;p&gt;During the years, a lot of people ask me for help in learning C++. I am no C++ expert, but as a person who is doing C++ for years, I want to share a bunch of beginner-friendly C++ resources that are known to be of high quality.&lt;/p&gt;

&lt;p&gt;When anyone asks me for guidance about getting into C++, I always ask first about their existing experiences. Some people just start to learn programming and decide to learn C++ as their first language, some people have learned a limited amount of C++ and want to learn more, and some of them are already programming veterans in other languages. Depend on your experiences, you probably want to start with different materials, and I try to cater to each of those backgrounds in this post.&lt;/p&gt;

&lt;p&gt;One thing I want to mention, though, is that reading books or watching videos all the time is not the best strategy to learn. Whatever stage you are in, it is much easier to learn when you apply ideas into code, so spending time on coding projects helps.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if I just start learning to program and choose C++ as my first language?
&lt;/h2&gt;

&lt;p&gt;For books, I recommend Bjarne Stroustrup (the creator of C++) 's &lt;a href="https://www.amazon.com/Programming-Principles-Practice-Using-2nd/dp/0321992784"&gt;"Programming: Principles and Practice Using C++ 2nd edition"&lt;/a&gt; as a starting point.&lt;br&gt;
The book is thick, so don't feel guilty if you cannot finish the whole book.&lt;/p&gt;

&lt;p&gt;If you are more inclined toward tutorial videos,&lt;br&gt;
look at Kate Gregory's &lt;a href="https://www.pluralsight.com/courses/learn-program-cplusplus"&gt;Learn to Program with C++&lt;/a&gt;. If you join the &lt;a href="https://www.includecpp.org/discord/"&gt;#include discord server&lt;/a&gt;, You can also message her there to get a trial code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if I already learned some C++ before and want to delve in deeper?
&lt;/h2&gt;

&lt;p&gt;What if you have some limited C++ experience before? Maybe you already learned some C++ from your university data structure course, or perhaps you followed some online tutorials that use C++. From my personal experiences and what I heard, most university programming courses or those online tutorials teach problematic practices, and the instructors often do not have a good grasp of the language. Thus, "learn from the best materials" is especially important for you to offset prior misconceptions on C++.&lt;/p&gt;

&lt;p&gt;For books, I will still recommend either Bjarne Stroustrup's &lt;a href="https://www.amazon.com/Programming-Principles-Practice-Using-2nd/dp/0321992784"&gt;"Programming: Principles and Practice Using C++ 2nd edition"&lt;/a&gt;. And for video tutorials, you can try Kate Gregory's &lt;a href="https://www.pluralsight.com/courses/cplusplus-fundamentals-c17"&gt;C++ Fundamentals Including C++ 17&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if I am a veteran in another language and want to delve into C++?
&lt;/h2&gt;

&lt;p&gt;If you are already a proficient programmer in some other languages and want to delve into C++, you will have a faster pace journey than other learners.&lt;/p&gt;

&lt;p&gt;As for book recommendations, Bjarne Stroustrup's &lt;a href="https://www.stroustrup.com/4th.html"&gt;"The C++ Programming Language (4th Edition)"&lt;/a&gt; was one of the best-written books I ever read, though do notice that this book was written with C++11 and misses some of the later developments. The book is also very thick, so if you want a shorter introduction, try &lt;a href="https://www.stroustrup.com/tour2.html"&gt;"A Tour of C++ (Second edition)"&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  I think that I have a decent grasp of C++. What's next?
&lt;/h2&gt;

&lt;p&gt;So you spend months with the above materials and feel that you have a decent grasp of basic C++ concepts.&lt;/p&gt;

&lt;p&gt;A sanity check to make sure about your understanding of C++ is whether you are familiar with the following topics, to name a few:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how to use &lt;code&gt;const&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;templates&lt;/li&gt;
&lt;li&gt;references and pointers&lt;/li&gt;
&lt;li&gt;usage of the standard library, in particular, iterator and algorithms&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.cppreference.com/w/cpp/language/raii"&gt;RAII&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;copy and move constructor and assignment&lt;/li&gt;
&lt;li&gt;move semantics&lt;/li&gt;
&lt;li&gt;operator overloading&lt;/li&gt;
&lt;li&gt;&lt;a href="https://lesleylai.info/en/c++-lambda"&gt;lambda expressions and function objects&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;undefined behaviors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now it is time to put C++ into practical usage. C++ is used for diverse purposes, and using C++ in specific areas is probably more critical than the C++ language itself. It is perhaps also a good time to spend some time on the broader C++ ecosystems, like unit test libraries such as &lt;a href="https://github.com/catchorg/Catch2"&gt;Catch2&lt;/a&gt;, build system generators such as &lt;a href="https://cmake.org/"&gt;CMake&lt;/a&gt;, and package managers such as &lt;a href="https://conan.io/"&gt;Conan&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another thing to consider is to start learning another programming language, especially for folks who only know C++ at this point. Good next languages to pick are those very different from C++, for example, dynamically-typed languages such as Javascript, Python, or a Lisp dialect.&lt;/p&gt;

&lt;p&gt;That being said, there is still &lt;em&gt;a lot&lt;/em&gt; to learn about the C++ language itself. And I will try to list some resources that are still relative up-to-date and I enjoyed:&lt;/p&gt;

&lt;h3&gt;
  
  
  Books
&lt;/h3&gt;

&lt;p&gt;If you haven't read &lt;a href="https://www.stroustrup.com/4th.html"&gt;"The C++ Programming Language (4th Edition)"&lt;/a&gt;, I would still recommend it. And here is a bunch of other books I would like to recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.amazon.com/Effective-Modern-Specific-Ways-Improve/dp/1491903996"&gt;"Effective Modern C++"&lt;/a&gt;  by Scott Mayer&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://leanpub.com/cppbestpractices"&gt;"C++ Best Practices"&lt;/a&gt; by Jason Turner&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.cppstd17.com/"&gt;"C++17 - The Complete Guide"&lt;/a&gt; by Nicolai M. Josuttis&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some books focus on specific areas of the language, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.tmplbook.com/"&gt;"C++ Templates - The Complete Guide, 2nd Edition"&lt;/a&gt; by David Vandevoorde, Nicolai M. Josuttis, and Douglas Gregor&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.amazon.com/Mastering-17-STL-standard-components/dp/178712682X"&gt;"Mastering the C++17 STL"&lt;/a&gt; by Arthur O'Dwyer&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.manning.com/books/functional-programming-in-c-plus-plus"&gt;"Functional Programming in C++"&lt;/a&gt; by Ivan Čukić&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.manning.com/books/c-plus-plus-concurrency-in-action-second-edition"&gt;"C++ Concurrency in Action, 2nd edition"&lt;/a&gt; by Anthony Williams&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conference Videos
&lt;/h3&gt;

&lt;p&gt;Conference videos are also an excellent resource to learn more about C++. They focus on a diversity of topics; many of them are hard to find in books. And they also require low commitment (just spend an hour lunchtime watching some videos)&lt;/p&gt;

&lt;p&gt;Here are some of my favorites that are also beginner-friendly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=MBRoCdtZOYg"&gt;CppCon 2019: Kate Gregory "Naming is Hard: Let's Do Better"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=n0Ak6xtVXno"&gt;CppCon 2018: Kate Gregory "Simplicity: Not Just For Beginners"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/XkDEzfpdcSg"&gt;CppCon 2017: Kate Gregory "10 Core Guidelines You Need to Start Using Now"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=bSkpMdDe4g4"&gt;CppCon 2017: Matt Godbolt "What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://channel9.msdn.com/Events/GoingNative/2013/Cpp-Seasoning"&gt;Going Native 2013: Sean Parent "C++ Seasoning"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/xnqTKD8uD64"&gt;CppCon 2014: Herb Sutter "Back to the Basics! Essentials of Modern C++ Style"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/1OEu9C51K2A"&gt;CppCon 2015: Bjarne Stroustrup “Writing Good C++14”&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/DHOlsEd0eDE"&gt;CppCon 2018: Jason Turner "Applied Best Practices"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/nnY4e4faNp0"&gt;CppCon 2017: Jason Turner “Practical C++17”&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Communities
&lt;/h3&gt;

&lt;p&gt;Many people in the C++ community and I, am always willing to answer direct messaging questions. However, I, or most people you can contact online, have limited experiences.&lt;/p&gt;

&lt;p&gt;To utilize the best wisdom of people, you need to join programming communities, and then you can ask questions in public and get a response from multiple people. &lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Being active in programming communities also has numerous other benefits, including getting job information and having more social support.&lt;/p&gt;

&lt;h4&gt;
  
  
  #include
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.includecpp.org/"&gt;#include&lt;/a&gt; is a wonderful community to join. Its mission includes providing conference scholarship to people in need, but for most people,&lt;br&gt;
you can join &lt;a href="https://www.includecpp.org/discord/"&gt;its discord server&lt;/a&gt; to hang out and talk about C++.&lt;/p&gt;

&lt;h4&gt;
  
  
  Local Meetups
&lt;/h4&gt;

&lt;p&gt;Joining &lt;a href="https://www.meetup.com/North-Denver-Metro-C-Meetup/"&gt;North Denver Metro C++ Meetup&lt;/a&gt; was one of the best decisions for me during my college years. I understand that it is a hard time to pop into meetups at the time of writing since most of them are currently held online. Nevertheless, I urge you to try to attend some meetups if you have time. Online meetings also provide some advantages compare to physical ones. For example, they require low commitment, and you can choose from all of those meetups worldwide.&lt;/p&gt;

&lt;h4&gt;
  
  
  Attending Conferences
&lt;/h4&gt;

&lt;p&gt;If you are serious about C++, then conferences are great places to meet like-minded people. Same as meetups, one difficulty at the time of writing is that most C++ conferences are hosted online, but they are still worthwhile to consider. Here are some of the recurring C++ conferences I am aware of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://cppcon.org/"&gt;CppCon&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://cppnow.org/"&gt;C++Now&lt;/a&gt; (tuned toward a more advanced audience)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://accu.org/conf-main/main/"&gt;ACCU&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://meetingcpp.com/"&gt;Meeting C++&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pacificplusplus.com/"&gt;Pacific++&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cpponsea.uk/"&gt;C++ on Sea&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://corecpp.org/"&gt;Core C++&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is &lt;a href="https://isocpp.org/wiki/faq/conferences-worldwide"&gt;a list of conferences&lt;/a&gt; on the ISO C++ website.&lt;/p&gt;

&lt;h4&gt;
  
  
  Listening to Podcasts
&lt;/h4&gt;

&lt;p&gt;There are a bunch of C++ podcasts, including a few new ones appeared in 2020:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.cppcast.com/"&gt;CppCast&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cpp.chat/"&gt;cpp.chat&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://tlbh.it/"&gt;TLB Hit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://adspthepodcast.com/"&gt;ADSP: The Podcast&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodiagnosticrequired.tv/"&gt;No Diagnostic Required&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.twoscomplement.org/"&gt;Two's Complement&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Following Blogs
&lt;/h4&gt;

&lt;p&gt;I use RSS to keep track of the tech blogs, and I highly recommend you try out RSS too.&lt;/p&gt;

&lt;p&gt;I follow hundreds of blogs, including C++ and various other topics. Here are some of the best C++ ones that pops into my head:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.fluentcpp.com/"&gt;Fluent C++&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://quuxplusone.github.io/blog/"&gt;Arthur O’Dwyer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://herbsutter.com/"&gt;Sutter’s Mill&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.modernescpp.com/"&gt;Modernes C++&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://thephd.github.io/"&gt;The Pasture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://foonathan.net/"&gt;foonathan::​blog()&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://brevzin.github.io/"&gt;Barry Revzin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://artificial-mind.net/"&gt;artificial::mind&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.bfilipek.com/"&gt;Bartek's coding blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://arne-mertz.de/"&gt;Simplify C++&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://videocortex.io/"&gt;Video Cortex&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://shafik.github.io/"&gt;Shafik Yaghmour&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do notice that blogs sometimes can talk about very advanced topics.&lt;/p&gt;

&lt;h4&gt;
  
  
  Twitter
&lt;/h4&gt;

&lt;p&gt;It is your personal preference on whether to join Twitter or not. On the one hand, Twitter is a great platform to directly communicate with the programming communities and know what other people are up to. And personally, Twitter is the platform that I know so many exciting developers worldwide. On the other hand, Twitter has its downside with all the procrastinating and doomscrolling. Some tweets you see can also make you upset. My suggestion is to try Twitter out at least, and you can quit if it doesn't work for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Misc resources
&lt;/h3&gt;

&lt;p&gt;Here are some misc resources that are also worth mentioning.&lt;br&gt;
Some of those are great online tools, while others are video series.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://en.cppreference.com/w/"&gt;cppreference&lt;/a&gt; should be your go-to site for C++ language and standard library reference, and it is usually a lot more accurate and up-to-date than its alternatives.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://compiler-explorer.com/"&gt;Compiler Explorer&lt;/a&gt; is an online coding environment that supports C++ and a dozen other languages. It can show the compiled assembly of your program and run your program. Unlike most online C++ coding environments, which often ship with an outdated compiler, there are many compilers to choose from in compiler explorer, including the most cutting-edge ones.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://quick-bench.com/"&gt;Quick C++ benchmark&lt;/a&gt; is an online tool to perform quick benchmarks on C++.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://abseil.io/tips/"&gt;C++ Tips of the week&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.pluralsight.com/courses/beautiful-cplusplus-stl-algorithms"&gt;Kate Gregory's STL Algorithms course&lt;/a&gt; is a great resource to learn more about and appreciate C++ standard algorithms.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/channel/UCxHAlbZQNFU2LgEtiqd2Maw"&gt;C++ Weekly&lt;/a&gt; is a Youtube channel on various C++ topics, posted weekly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References and further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;"SG20 Education And Recommended Videos For Teaching C++". Christopher Di Bella&lt;/em&gt;, 2021, &lt;a href="https://www.cjdb.com.au/sg20-and-videos"&gt;https://www.cjdb.com.au/sg20-and-videos&lt;/a&gt;. Accessed 15 Jan 2021.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;"References And Links". #Include ＜C++＞&lt;/em&gt;, 2021, &lt;a href="https://www.includecpp.org/resources/references/"&gt;https://www.includecpp.org/resources/references/&lt;/a&gt;. Accessed 16 Jan 2021.&lt;/li&gt;
&lt;li&gt;Yaghmour, Shafik. &lt;em&gt;"Where To Get Started Learing C++ And What Resources To Use". Shafik Yaghmour's Blog&lt;/em&gt;, 2019, &lt;a href="https://shafik.github.io/c++/learning/2019/09/05/getting_started_learning_cpp.html"&gt;https://shafik.github.io/c++/learning/2019/09/05/getting_started_learning_cpp.html&lt;/a&gt;. Accessed 16 Jan 2021.&lt;/li&gt;
&lt;/ul&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Asking questions online is an art, and a poorly phrased question makes people don't know how to respond. Further, people are often too polite to point out that a question is poorly phrased. Kate Gregory's &lt;a href="http://www.gregcons.com/KateBlog/HowToAskForCCodingHelp.aspx"&gt;How to ask for C++ coding help&lt;/a&gt; is an excellent read on how to ask for help online. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>cpp</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>C++ Lambda Tutorial</title>
      <dc:creator>Lesley Lai</dc:creator>
      <pubDate>Mon, 13 Jan 2020 00:16:34 +0000</pubDate>
      <link>https://dev.to/lesleylai/c-lambda-tutorial-4204</link>
      <guid>https://dev.to/lesleylai/c-lambda-tutorial-4204</guid>
      <description>&lt;p&gt;C++ lambda expressions are a construct added to C++ back in C++11, and it continues to evolve in each version of the C++ standard. A core part of the language nowadays, lambda expressions enable programmers of writing anonymous functions in C++. This post describes what a lambda is, provides some basic usages, and outlines their benefits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Usage
&lt;/h2&gt;

&lt;p&gt;Passing functions as a parameter to customize the behavior of functions is a common task in programming. For example, since the conception of &lt;a href="https://en.cppreference.com/w/cpp/algorithm"&gt;standard algorithms library&lt;/a&gt;, a lot of the algorithms in the &lt;code&gt;&amp;lt;algorithm&amp;gt;&lt;/code&gt; can take an invokable entity as a callback. However, before C++11, the only kinds of invokable entities in C++ are function pointers and function objects. Both of them require quite a bit of boilerplate, and this cumbersomeness even impedes the adaption of the standard algorithm library in practice.&lt;/p&gt;

&lt;p&gt;On the meantime, lots of programming languages support features of &lt;a href="https://en.wikipedia.org/wiki/Anonymous_function"&gt;anonymous functions&lt;/a&gt;. Before C++11, such features are achieved by metaprogramming. For example, the Boost C++ library provided its &lt;a href="http://www.boost.org/libs/lambda"&gt;boost.lambda&lt;/a&gt; library. Those metaprogramming hacks are slow to compile and not performant; moreover, they require more boilerplate then one want. Thus, in C++11, lambda expressions are added as a language extension. The ISO C++ Standard shows usage of a lambda expression as a comparator of the &lt;code&gt;sort&lt;/code&gt; algorithm. &lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;cmath&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;abssort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the function &lt;code&gt;abssort&lt;/code&gt;, we passed lambda into &lt;code&gt;std::sort&lt;/code&gt; as a comparator. We can write a normal function to achieve the same purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;cmath&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;abs_less&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;abssort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abs_less&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We still do not know what the strange &lt;code&gt;[]&lt;/code&gt; syntax is for, and that is our topic next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Captures
&lt;/h2&gt;

&lt;p&gt;The above example shows the basic usage of Lambdas, but Lambdas can do more. The main difference between a lambda and a regular function is that it can "capture" state, and then we can use the captured value inside the lambda body. For example, the below function gets a new vector with the element above a certain number in the old vector.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Get a new vector&amp;lt;int&amp;gt; with element above a certain number in the old vector&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filter_above&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;copy_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;back_insert_iterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;;});&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// filter_above(std::vector&amp;lt;int&amp;gt;{0, 1, 2, 4, 8, 16, 32}, 5) == std::vector&amp;lt;int&amp;gt;{8, 16, 32}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code captures &lt;code&gt;threshold&lt;/code&gt; by value. The &lt;code&gt;[]&lt;/code&gt; construct is called a &lt;em&gt;capture clause&lt;/em&gt;. There are two kinds of captures, capture by value or capture by reference (&lt;code&gt;[&amp;amp;]&lt;/code&gt;). For example, &lt;code&gt;[x, &amp;amp;y]&lt;/code&gt; - capture &lt;code&gt;x&lt;/code&gt; by value and &lt;code&gt;y&lt;/code&gt; by a reference explicitly. You can also have a default capture clause, for example,  &lt;code&gt;[=]&lt;/code&gt; captures everything in the current environment by value and &lt;code&gt;[&amp;amp;]&lt;/code&gt; captures everything by reference.&lt;/p&gt;

&lt;p&gt;A function that store an environment is called a &lt;a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)"&gt;&lt;em&gt;closure&lt;/em&gt;&lt;/a&gt;; almost all modern programming languages support closures. However, in all languages that I know except C++, the capture list is implicit. In those languages, a closure captures all the bindings from the current environment.&lt;/p&gt;

&lt;p&gt;We can mimic the behaviors in those languages by capturing everything by reference (&lt;code&gt;[&amp;amp;]&lt;/code&gt;), and it captures all the variables in the environment that are used in the lambda. However, default capture can be dangerous in C++; it leads to potential dangling problems if the lambda lives longer than the captured object. For example, we can pass a callback to asynchronous functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="nf"&gt;greeter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Lesley"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;async&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;](){&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is undefined behavior since &lt;code&gt;name&lt;/code&gt; may be destroyed when we execute the asynchronous operation. The rule of thumb is only to use default capture by reference when the lambda is short-lived. For example, when passing a lambda to STL algorithms.&lt;/p&gt;

&lt;p&gt;The implicit capture strategy works in garbage-collected languages. &lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt; gets away with implicit capture because of its borrow checker. On the contrary, by requiring the programmer to be explicit about ownership, the C++ approach provides more flexibility than the counterparts in other programming languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Lambda
&lt;/h2&gt;

&lt;p&gt;We discussed quite a lot of usage of Lambda so far. However, curious readers may start to wonder, what &lt;em&gt;exactly&lt;/em&gt; is a C++ Lambda? Is it a primitive language construct like closures in functional languages? However, before I talk about the internal of Lambda, I will first talk about a construct date back to C++98 era, &lt;strong&gt;function objects&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Object
&lt;/h3&gt;

&lt;p&gt;Function objects are normal objects that are able to be invoked. They are implemented by overloading a class' &lt;code&gt;operator()&lt;/code&gt; operator. Below is our &lt;code&gt;abs_less&lt;/code&gt; example as a function object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;cmath&amp;gt;
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;abs_less&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="p"&gt;()(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;abssort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abs_less&lt;/span&gt;&lt;span class="p"&gt;{});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Function objects are more flexible than regular functions because they can store data like ordinary objects. Let us implement the previous &lt;code&gt;filter_above&lt;/code&gt; example with function object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GreaterThan&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="n"&gt;GreaterThan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;threshold_&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="p"&gt;()(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;noexcept&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;threshold_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nl"&gt;private:&lt;/span&gt;
  &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;threshold_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filter_above&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;copy_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;back_insert_iterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;GreaterThan&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I am using Class template argument deduction (CTAD) in this snippet. CTAD is a C++17 feature. In the previous versions, we need to write &lt;code&gt;GreaterThan&amp;lt;int&amp;gt;{threshold}&lt;/code&gt; with the template parameter &lt;code&gt;int&lt;/code&gt; specified.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Going back to lambda
&lt;/h3&gt;

&lt;p&gt;Lambdas in C++ are syntactic sugars of function objects. In other word, the compilers translate lambda expressions into function objects. Through the amazing &lt;a href="https://cppinsights.io/"&gt;C++ Insights&lt;/a&gt; website, we can see a desugared version of our &lt;code&gt;abssort&lt;/code&gt; example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;cmath&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;abssort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;__lambda_6_9&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;public:&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="cm"&gt;/*constexpr */&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt;&lt;span class="p"&gt;()(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__lambda_6_9&lt;/span&gt;&lt;span class="p"&gt;{});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lambda is merely a default constructed object of a &lt;a href="https://en.cppreference.com/w/cpp/language/class#Local_classes"&gt;local class&lt;/a&gt;. Thus, C++ Lambda can do a lot of stuff anonymous functions in other languages may not allow to do. For example, you can inherit from lambda and have mutable states from lambda. Though I haven't found too much use for either of them.&lt;/p&gt;

&lt;p&gt;The compilers generate the types of Lambdas; however, there is no way to use such types by their name through any standard means in a program. Nonetheless, type inferences and template works just normal for those types. Also, we can use those types explicitly by &lt;code&gt;decltype&lt;/code&gt;. Below is an example from the &lt;a href="https://en.cppreference.com/w/cpp/language/decltype"&gt;cppreference&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;decltype&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Such anonymous types are called "&lt;em&gt;Voldemort's types&lt;/em&gt;" in the world of C++ and the &lt;a href="https://dlang.org/"&gt;D programming language&lt;/a&gt; because they cannot be directly named, but codes can still use this type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Capture with an initializer
&lt;/h2&gt;

&lt;p&gt;Now you understand a Lambda is a function object; you may expect Lambdas to store arbitrary values, not just to capture the values from their local scope. Fortunately, in C++14, lambdas can introduce new variables in its body by the mean of capturing with an &lt;em&gt;initializer&lt;/em&gt;&lt;sup id="fnref2"&gt;2&lt;/sup&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 1 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Move capture
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt; lambdas can take ownership of the values in the environment. C++ lambdas do not have special support for such &lt;em&gt;move capture&lt;/em&gt;, but the generalized capture in the C++14 covers such use case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// a unique_ptr is move-only&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;make_unique&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;some_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;some&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// move the unique_ptr into the lambda&lt;/span&gt;
&lt;span class="n"&gt;go&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;do_something_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Immediately Invoked Lambda
&lt;/h2&gt;

&lt;p&gt;You can invoke lambda at the same place where we construct them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;[]()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}();&lt;/span&gt; &lt;span class="c1"&gt;// Same as what is inside the curly braces&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the world of Javascript, immediately invoked function expressions are all over the place. JavaScript programmers use them to introduce scopes. C++ does not need this kind of trickery. As a result, C++ programmers are more reluctant to use immediately invoked lambda. For example, in her &lt;a href="https://www.youtube.com/watch?v=n0Ak6xtVXno"&gt;talk&lt;/a&gt; during CppCon 2018, Kate Gregory concerns about the readability of the immediately invoked lambda for people not familiar with this idiom.&lt;/p&gt;

&lt;p&gt;Nevertheless, if you follow the best practice of declaring as more &lt;code&gt;const&lt;/code&gt; values as possible, immediately invoked lambda does provide an advantage. Some objects require complex construction beyond the constructor's capability. Mutations will only happen during the construction of objects. Once the construction is completed, the objects will never be modified again. If such construction is reusable, then writing builder classes or factory functions is a sane choice. However, if such construction only happens once in the codebase, a lot of the people will drop the &lt;code&gt;const&lt;/code&gt; qualifier instead. For example, consider that if you want to read several lines from &lt;code&gt;stdin&lt;/code&gt; into a vector:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It seems no way to make &lt;code&gt;lines&lt;/code&gt; constant since we need to modify it in the loop. Immediately invoked lambda solves this dilemma. With it, you can have both &lt;code&gt;const&lt;/code&gt; and no boilerplates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;getline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This post is first published on &lt;a href="https://lesleylai.info/en/c++-lambda"&gt;Lesley Lai's Blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;See &lt;a href="http://eel.is/c%2B%2Bdraft/expr.prim.lambda#1"&gt;&lt;strong&gt;[expr.prim.lambda]&lt;/strong&gt;&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://isocpp.org/wiki/faq/cpp14-language#lambda-captures"&gt;C++14 Language Extensions: Generalized lambda captures&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>cpp</category>
    </item>
    <item>
      <title>Const Correctness Issue of std::function</title>
      <dc:creator>Lesley Lai</dc:creator>
      <pubDate>Mon, 30 Dec 2019 09:39:50 +0000</pubDate>
      <link>https://dev.to/lesleylai/const-correctness-issue-of-std-function-4mji</link>
      <guid>https://dev.to/lesleylai/const-correctness-issue-of-std-function-4mji</guid>
      <description>&lt;p&gt;The &lt;code&gt;const&lt;/code&gt; type qualifier is one of the jewels of the C++ language design. Surrounding by this feature, we devise the "&lt;code&gt;const&lt;/code&gt; correctness" practice to prevent &lt;code&gt;const&lt;/code&gt; objects from getting mutated. The &lt;code&gt;const&lt;/code&gt; correctness rule is straight-forward to follow for implementation of the most classes, but it is harder to heed for classes with type erasure. Unfortunately, the standard library type &lt;a href="https://en.cppreference.com/w/cpp/utility/functional/function"&gt;&lt;code&gt;std::function&lt;/code&gt;&lt;/a&gt; is implemented by type erasure; and due to short-sightedness, it becomes one of the ill-behaved citizens that doesn't follow the const-correctness rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;std::function&lt;/code&gt; has one const member &lt;code&gt;operator()&lt;/code&gt;, yet it can mutate the underlying function. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}};&lt;/span&gt;
&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// returns 1&lt;/span&gt;
&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// returns 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Document N4348&lt;sup id="fnref1"&gt;1&lt;/sup&gt; first formalized this concern. It states that&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This violates not only basic tenets of &lt;code&gt;const-&lt;/code&gt;correctness, but also the data race avoidance guarantees in [res.on.data.races]/p3, which states that "A C++ standard library function shall not directly or indirectly modify objects accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function's non-&lt;code&gt;const&lt;/code&gt; arguments, including &lt;code&gt;this&lt;/code&gt;".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Fix
&lt;/h2&gt;

&lt;p&gt;Implementations of a &lt;code&gt;function&lt;/code&gt;-like class should have separate specializations for &lt;code&gt;const&lt;/code&gt; and non-&lt;code&gt;const&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sig&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;function&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// not defined&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="nc"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;operator()&lt;/code&gt; of the &lt;code&gt;const&lt;/code&gt; specialization should be annotated as &lt;code&gt;const&lt;/code&gt;, but the constructor of the &lt;code&gt;const&lt;/code&gt; specialization would not accept mutable function objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f1&lt;/span&gt; &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}};&lt;/span&gt;
&lt;span class="n"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// ok;&lt;/span&gt;

&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f2&lt;/span&gt; &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}};&lt;/span&gt; &lt;span class="c1"&gt;// Does not compile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, &lt;code&gt;operator()&lt;/code&gt; of the non-&lt;code&gt;const&lt;/code&gt; specialization would not have &lt;code&gt;const&lt;/code&gt; type signature, so you cannot invoke the &lt;code&gt;const&lt;/code&gt; version of such functions at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f1&lt;/span&gt; &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}};&lt;/span&gt;
&lt;span class="n"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ok&lt;/span&gt;

&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f2&lt;/span&gt; &lt;span class="p"&gt;{[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}};&lt;/span&gt;
&lt;span class="n"&gt;f2&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Does not compile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Future
&lt;/h2&gt;

&lt;p&gt;I don't expect &lt;code&gt;std::function&lt;/code&gt; itself to have any change that breaks backward-compatibility. As of the time of this writing (December 2019), my bet is on the proposed &lt;code&gt;std::unique_function&lt;/code&gt; &lt;sup id="fnref2"&gt;2&lt;/sup&gt;, which is a drop-in replacement of &lt;code&gt;std::function&lt;/code&gt; that fixes the const-correctness bug among other features. Once we have an alternative in standard, &lt;code&gt;std::function&lt;/code&gt; can be deprecated just like &lt;code&gt;std::auto_function&lt;/code&gt;. In the meantime, we can always implement &lt;code&gt;unique_ptr&lt;/code&gt; on our own, and I have a small library to implement that on &lt;a href="https://github.com/Beyond-Engine/functions"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post is first published on &lt;a href="https://lesleylai.info/en/const-correcness-std-function"&gt;Lesley Lai's Blog&lt;/a&gt;.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4348.html"&gt;Making std::function safe for concurrency&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0228r3.html"&gt;P0228R3 unique_function: a move-only std::function&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>cpp</category>
    </item>
  </channel>
</rss>
