Udostępnij za pośrednictwem


Lambda not just for Lambs dad’s anymore

Lambda

Well that is a stupid title.  Sorry. See it is a visual pun, Lamb dad… yep. <Awkward pause>

Moving on:

What is a Lambda expression?

“Lambda expressions provide a concise way to create simple function objects. [ISO/IEC14882.2011(E) Paragraph 5.1.2]

Why to use Lambda?

By using lambdas, you can write code that's less cumbersome and less prone to errors than the code for an equivalent function object.

Format

Good link on formatting: https://msdn.microsoft.com/en-us/library/dd293603(v=vs.120).aspx

  • [a,&b] where a is captured by value and b is captured by reference.
  • [this] captures the this pointer by value
  • [&] captures all automatic variables mentioned in the body of the lambda by reference
  • [=] captures all automatic variables mentioned in the body of the lambda by value
  • [] captures nothing

 

How to use a Lambda?

I found a pretty good tutorial to get started with at: https://solarianprogrammer.com/2011/11/01/cpp-11-lambda-tutorial/ 

Examples of many of the variations of Lambda expressions: https://msdn.microsoft.com/en-us/library/dd293599(v=vs.120).aspx

Resource utilization

Lambda’s allow you to save time writing them, and the resource overhead is stated to be less than the comparable function body approach.  Lambda Expressions can only be used in limited situation and they can make code difficult to read if not documented well or written with others in mind.  Lambda expressions are definitely more concise and should be used where appropriate.