Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the motivation behind C++11 lambda expressions?

Tags:

c++

c++11

lambda

I am trying to find out if there is an actual computational benefit to using lambda expressions in C++, namely "this code compiles/runs faster/slower because we use lambda expressions" or is it just a neat development perk open for abuse by poor coders trying to look cool?

I understand this question may seem subjective, but I would much appreciate the opinion of the community on this matter.

like image 297
LoudNPossiblyWrong Avatar asked Jun 10 '10 21:06

LoudNPossiblyWrong


People also ask

What is a lambda expression in C++11?

In C++11 and later, a lambda expression—often called a lambda—is a convenient way of defining an anonymous function object (a closure) right at the location where it's invoked or passed as an argument to a function.

What is the purpose of LaMDA?

LaMDA is designed to do one thing: provide conversational responses that make sense and are specific to the context of the dialogue. That can give it the appearance of being sentient, but as Jeff says, it's essentially lying.

Why do we need lambdas in C++?

Summary and References. We have seen that lambda is just a convenient way to write a functor, therefore we should always think about it as a functor when coding in C++. We should use lambdas where we can improve the readability of and simplify our code such as when writing callback functions.

What does [&] mean in C++?

It's a lambda capture list and has been defined in C++ from the C++11 standard. [&] It means that you're wanting to access every variable by reference currently in scope within the lambda function.


4 Answers

The benefit is what's the most important thing in writing computer programs: easier to understand code. I'm not aware of any performance considerations.

C++ allows, to a certain extend, to do Functional Programming. Consider this:

std::for_each( begin, end, doer );

The problem with this is that the function (object) doer

  1. specifies what's done in the loop
  2. yet somewhat hides what's actually done (you have to look up the function object's operator()'s implementation)
  3. must be defined in a different scope than the std::for_each call
  4. contains a certain amount of boilerplate code
  5. is often throw-away code that's not used for anything but this one loop construct

Lambdas considerably improve on all these (and maybe some more I forgot).

like image 59
sbi Avatar answered Oct 02 '22 20:10

sbi


I don't think it's nearly as much about the computational performance as increasing the expressive power of the language.

like image 24
JaredPar Avatar answered Oct 02 '22 20:10

JaredPar


There's no performance benefit per se, but the need for lambda came as a consequence of the wide adoption of the STL and its design ideas.

Specifically, the STL algorithms make frequent use of functors. Without lambda, these functors need to be previously declared to be used. Lambdas make it possible to have 'anonymous', in-place functors.

This is important because there are many situations in which you need to use a functor only once, and you don't want to give a name to it for two reasons: you don't want to pollute the namespace, and in those specific cases the name you give is either vague or extremely long.

I, for instance, use STL a lot, but without C++0x I use much more for() loops than the for_each() algorithm and its cousins. That's because if I were to use for_each() instead, I'd need to get the code from inside the loop and declare a functor for it. Also all the local variables before the loop wouldn't be accessible, so I'd need to write additional code to pass them as parameters to the functor constructor, or another thing equivalent. As a consequence, I tend not to use for_each() unless there's strong motivation, otherwise the code would be longer and more difficult to read.

That's bad, because it's well known that using for_each() and similar algorithms gives much more room to the compiler & the library for optimizations, including automatic parallelism. So, indirectly, lambda will favour more efficient code.

like image 21
Fabio Ceconello Avatar answered Oct 02 '22 21:10

Fabio Ceconello


IMO, the most important thing about lambda's is it keeps related code close together. If you have this code:

std::for_each(begin, end, unknown_function);

You need to navigate over to unknown_function to understand what the code does. But with a lambda, the logic can be kept together.

like image 21
R Samuel Klatchko Avatar answered Oct 02 '22 22:10

R Samuel Klatchko