{"id":5204,"date":"2017-06-28T20:22:46","date_gmt":"2017-06-28T20:22:46","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5204"},"modified":"2017-06-28T20:23:38","modified_gmt":"2017-06-28T20:23:38","slug":"c-lambdas-in-a-little-more-depth","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/c-lambdas-in-a-little-more-depth\/","title":{"rendered":"C++ lambda&#8217;s in a little more depth"},"content":{"rendered":"<p><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/lambda\" target=\"_blank\">Lambda expressions<\/a> appeared in C++ 11.<\/p>\n<p>Let&#8217;s take a simple example. We want to create a lambda which takes an enum (of type  UpdateFlag) and returns a LPTSTR (yes we&#8217;re in Visual C++ world) which we&#8217;ll use when we writing to cout.<\/p>\n<p>We can create the lambda like this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nauto lambda = &#x5B;](UpdateFlag flag)\r\n{\r\n   switch (flag)\r\n   {\r\n      case UpdateFlag::Update:\r\n         return _T(&quot;Update&quot;);\r\n      case UpdateFlag::Delete:\r\n         return _T(&quot;Delete&quot;);\r\n      case UpdateFlag::Missing:\r\n         return _T(&quot;Missing&quot;);\r\n   }\r\n};\r\n<\/pre>\n<p>if you prefer to not use the <em>auto<\/em> keyword we can use the <em>functional<\/em> code like this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstd::function&lt;LPCTSTR(UpdateFlag)&gt; lambda = &#x5B;](UpdateFlag flag)\r\n{\r\n\/\/ switch removed for brevity\r\n};\r\n<\/pre>\n<p><em>I&#8217;m not quite sure where you&#8217;d need to use the std::function variation of this, but this is what it would look like.<\/em><\/p>\n<p>Now to use our lambda, we&#8217;d simply have something like<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstd::cout &lt;&lt; lamba(updateFlag);\r\n<\/pre>\n<p>We can also pass the lambda (without creating a variable to it) using the following<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstd::cout &lt;&lt; &#x5B;](UpdateFlag flag)\r\n{\r\n\/\/ switch removed for brevity\r\n}(updateFlag);\r\n<\/pre>\n<p><em>Note: we need to pass the updateFlag in a parameter list after the lamba declaration.<\/em><\/p>\n<p>The [] is an empty capture list, we could alter the previous samples (assuming a variable updateFlag is available within the scope of the lambda) by passing the updateFlag as part of the capture list, for example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nauto updateFlag = UpdateFlag::Update;\r\n\r\nauto lambda = &#x5B;updateFlag]\r\n{\r\n   switch (updateFlag)\r\n   {\r\n      case UpdateFlag::Update:\r\n         return _T(&quot;Update&quot;);\r\n      case UpdateFlag::Delete:\r\n         return _T(&quot;Delete&quot;);\r\n      case UpdateFlag::Missing:\r\n         return _T(&quot;Missing&quot;);\r\n   }\r\n};\r\n<\/pre>\n<p><strong>Structuring the lamba<\/strong><\/p>\n<p>Let&#8217;s look at the format of the lamba&#8230;<\/p>\n<p>The lamba syntax basically takes the form<\/p>\n<ul>\n<li>[capture-list] (params) mutable(optional) constexpr(optional)(c++17) exception attribute -> ret { body }<\/li>\n<li>[capture-list](params) -> ret { body }<\/li>\n<li>[capture-list](params) { body }<\/li>\n<li>[capture-list] { body }<\/li>\n<\/ul>\n<p><em>The above is replicated from the <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/lambda\" target=\"_blank\">Lambda expressions<\/a> page.<\/em><\/p>\n<p>The capture-list is a comma seperated list of zero or more captures, which takes or &#8220;captures&#8221; variables to be passed into the lambda.<\/p>\n<p>The params are a standard list of parameters to pass into your lambda and the body is your actual code. <\/p>\n<p><strong>References<\/strong><\/p>\n<p>An excellent article on the subject of C++ 11 lambda&#8217;s can be found <a href=\"http:\/\/www.drdobbs.com\/cpp\/lambdas-in-c11\/240168241\" target=\"_blank\">here<\/a>.<br \/>\nThe lambda specification can be found <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/lambda\" target=\"_blank\">here<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lambda expressions appeared in C++ 11. Let&#8217;s take a simple example. We want to create a lambda which takes an enum (of type UpdateFlag) and returns a LPTSTR (yes we&#8217;re in Visual C++ world) which we&#8217;ll use when we writing to cout. We can create the lambda like this auto lambda = &#x5B;](UpdateFlag flag) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[180],"tags":[],"class_list":["post-5204","post","type-post","status-publish","format-standard","hentry","category-cc"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5204"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5204\/revisions"}],"predecessor-version":[{"id":5218,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5204\/revisions\/5218"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}