Show lambda with parameters are used? How to pass parameters to them?
It works exactly like with any other type of callable object:
#include <iostream>
int main()
{
auto l = [] (int i) { std::cout << "The answer is " << i; };
l(42);
}
Also notice, that you do not need to store a lambda in a variable in order to invoke it. The following is an alternative way to rewrite the above program:
#include <iostream>
int main()
{
[] (int i) { std::cout << "The answer is " << i; } (42);
// ^^^^
// Invoked immediately!
}
The type of a lambda function (the so-called "lambda closure") is defined by the compiler, and is a functor with a call operator whose signature is the one you specify when defining the lambda. Therefore, you call a lambda exactly as you would call a functor (i.e. exactly as you would call a function - or any callable object).
Thus, if you want to assign a lambda to an object, the best practice is to let the compiler deduce its type by using auto. If you do not want or cannot use auto, then you may:
Use function pointers for non-capturing lambdas (capturing lambdas are not convertible to function pointers). In the above case, thus, the following will also work:
#include <iostream> int main() { void (*f)(int) = [] (int i) { std::cout << "The answer is " << i; }; f(42); }Use
std::function(this is always possible, even if the lambda is capturing):#include <iostream> #include <functional> int main() { std::function<void(int)> f = [] (int i) { std::cout << "The answer is " << i; }; f(42); }
Show lambda with parameters are used? How to pass parameters to them?
It works exactly like with any other type of callable object:
#include <iostream>
int main()
{
auto l = [] (int i) { std::cout << "The answer is " << i; };
l(42);
}
Also notice, that you do not need to store a lambda in a variable in order to invoke it. The following is an alternative way to rewrite the above program:
#include <iostream>
int main()
{
[] (int i) { std::cout << "The answer is " << i; } (42);
// ^^^^
// Invoked immediately!
}
The type of a lambda function (the so-called "lambda closure") is defined by the compiler, and is a functor with a call operator whose signature is the one you specify when defining the lambda. Therefore, you call a lambda exactly as you would call a functor (i.e. exactly as you would call a function - or any callable object).
Thus, if you want to assign a lambda to an object, the best practice is to let the compiler deduce its type by using auto. If you do not want or cannot use auto, then you may:
Use function pointers for non-capturing lambdas (capturing lambdas are not convertible to function pointers). In the above case, thus, the following will also work:
#include <iostream> int main() { void (*f)(int) = [] (int i) { std::cout << "The answer is " << i; }; f(42); }Use
std::function(this is always possible, even if the lambda is capturing):#include <iostream> #include <functional> int main() { std::function<void(int)> f = [] (int i) { std::cout << "The answer is " << i; }; f(42); }
auto lambda = [] (int a, int b) { return a + b; };
assert(lambda(1, 2) == 3);
Use a lambda as a parameter for a C++ function - Stack Overflow
When to use a lambda over a function pointer?
c++ - Proper way to receive a lambda as parameter by reference - Stack Overflow
How do I pass a lambda to a method as a parameter?
Videos
You have 2 ways: make your function template:
template <typename F>
void myFunction(F&& lambda)
{
//some things
}
or erase type (with std::function for example):
void
myFunction(const std::function<void()/*type of your lamdba::operator()*/>& f)
{
//some things
}
You have two choices, basically.
Make it a template:
template<typename T>
void myFunction(T&& lambda){
}
or, if you do not want (or can't) do that, you can use type-erased std::function:
void myFunction(std::function<void()> const& lambda){
}
Conversely, your attempt with auto would've been correct under the concepts TS as currently implemented in gcc, where it'd be an abbreviated template.
// hypothetical C++2x code
void myFunction(auto&& lambda){
}
or with a concept:
// hypothetical C++2x code
void myFunction(Callable&& lambda){
}
Question seems simple enough.
I've never really understood then point of lambdas. To me the code seems messy, why not use a function pointer instead?
You cannot have an auto parameter. You basically have two options:
Option #1: Use std::function as you have shown.
Option #2: Use a template parameter:
template<typename F>
void f(F && lambda) { /* ... */}
Option #2 may, in some cases, be more efficient, as it can avoid a potential heap allocation for the embedded lambda function object, but is only possible if f can be placed in a header as a template function. It may also increase compile times and I-cache footprint, as can any template. Note that it may have no effect as well, as if the lambda function object is small enough it may be represented inline in the std::function object.
Don't forget to use std::forward<F&&>(lambda) when referring to lambda, as it is an r-value reference.
I would use template as:
template<typename Functor>
void f(Functor functor)
{
cout << functor(10) << endl;
}
int g(int x)
{
return x * x;
}
int main()
{
auto lambda = [] (int x) { cout << x * 50 << endl; return x * 100; };
f(lambda); //pass lambda
f(g); //pass function
}
Output:
500
1000
100
Demo : http://www.ideone.com/EayVq