Anonymous Function
Anonymous functions don’t have names. I first learned about this at Marianopolis College programming class, but it was actually super easy.
I wonder if every single person in that class went into Software Engineering / Computer Science lol.
Python
Something like this
C++
Used for the first time when I was trying to write ORB-SLAM from scratch.
Anatomy of lambda function
- Capture clause (Also known as the lambda-introducer in the C++ specification.)
- Parameter list Optional. (Also known as the lambda declarator)
- Mutable specification Optional.
- Exception-specification Optional.
- trailing-return-type Optional.
- lambda body.
In this case tot
is captured by value. C++11 lambdas support capturing by:
- value
[x]
- reference
[&x]
- any variable currently in scope by reference
[&]
- same as 3, but by value
[=]
You can mix any of the above in a comma separated list [x, &y]
.
Tip
Using the third way
[&]
is generally considered bad practice, because you aren’t explicit about the variables that you want to use.
I don't get the capture clause vs. parameter list? What is the difference?
The parameter list is the list of parameters you pass to the lambda function when it comes times to calling it. The capture clause lists out variables that can be used inside the lambda function (essentially extending the scope of the variable).
Why use the capture clause when it can be passed in the parameter list?
This is a good question. I found a stack overflow answer.
Here’s a good example of where you need capture clause:
- in this case,
for_each
expects a function that takes in a single value of the type of array, and applies it - You can’t pass
r
into the argument list, since when thefor_each
function is defined, it works for aUnaryFunction
(see below)- It’s sort of equivalent to using std::bind (C++)
Capture clauses thus introduce a new level of abstraction that one can make use of.
This is how it’s implemented under the hood
### Related
Example
```cpp
auto myLambda = [](int a, int b) {
return a + b;
};
myLambda(5, 10); // 15
With a capture clause
Are we supposed to use the
auto
keyword?Ya. Under the hood, its a
std::function
.
To access local variables from within anonymous function, you need a Closure.