Case Study for Lambdas

I recently started the process of researching lambda for personal use. I have been coding for several years, Ive released several products into the wild, and yet, Ive never found that I need to use lambda. Ive read other stack exchange answers to various lambda questions, but I have not found an explanation that shows a simple example that really stimulates the idea that lambdas are necessary (for some situations). After a little research, Im not convinced that some of the specific applications of lambdas cannot be implemented with standard functions, however this does not mean much, because my scope of the topic is extremely limited.

Can someone provide a fairly simple example of using a lambda that demonstrates how they can be more powerful than regular functions (in the right situations)?

+7
c ++ lambda
source share
3 answers

C ++ was Turing completed long before C ++ 11. A lambda does not exist to do something impossible. You can do a lot without them. They are designed to optimize some code fragments.

For example, to provide the ability to create functors on the fly in the standard library of algorithms. Of course, you can determine one at your discretion. But this requires going beyond your function and the name of a new type. As an example, there are many templates for a simple call to std::transform .

They keep the code localized, don't require you to pollute any names, and are easy to use when you hang it.


As a bonus, in his talk about functional C ++ , Kevlin Henny reduces an example function from a framework to something that really screams “use lambda.” Therefore, to borrow his conversation a little, consider the function of registering commands:

 void register_command(std::string cmdName, std::function<void()> f); 

You want to register a command that simply turns on the light bulb. How many templates do you need to write for a custom functor compared to the following?

 register_command("Turn On Light-bulb", [&light_controller]() { light_controller.on(); }); 

Think about it, and which code base would you rather support.

+14
source share

All that can be done with lambdas these days could be done in C ++ 03 by placing struct / class with operator() overloaded (usually called "functors"). There was a huge problem: lack and lack of locality .

Imagine that you sent a template predicate to something like std::find_if in C ++ 03:

 struct predicate { template <typename T> bool operator()(const T& x) { return boop(x); } }; void foo() { something(std::find_if(c.begin(), c.end(), predicate())); } 

You need:

  • Define a predicate somewhere potentially far from where it was used.

  • Whether the shell and operator() equipped with a shell for overloading.

  • Jump in the source code while reading to understand what predicate does.

Compare this with the generic C ++ 14 lambda:

 void foo() { something(std::find_if(c.begin(), c.end(), [](const auto& x){ return boop(x); }); } 

Boom. Everything is there, locally, with a minimal template.


Lambda is not magic. They are pure syntactic sugar. But I believe that they are "syntactic sugar for struct s handwriting," because "C ++ is syntactic sugar for C".

They change the way you write code, they make functional paradigms (like higher-order functions) viable, and they can make your code more powerful and safe.

+4
source share

We can do almost anything without using lambda. However, lambdas are implemented to support functional programming and are useful in many cases.

I recently used it in threads and it made my life easier. Here is a very simplified version

 int main() { int value = 0; thread th { [=](){ cout<<"I am inside thread fn Value = "<<value<<endl; }}; th.join(); } 

There are two obvious advantages here:

  • Lambda prevented the creation of a global or class static function
  • I don't need to pass parameters to threads (a difficult task), because closing the lambda will take care of that.

So, there are several cases where lambda may be the best option, but that does not mean that we cannot do the same with functions

+2
source share

All Articles