C++11 lambda function - how to pass parameter
Solution 1
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); }
Solution 2
auto lambda = [] (int a, int b) { return a + b; };
assert(lambda(1, 2) == 3);
Solution 3
You don't even need a variable to hold your lambda -- you can call it directly:
std::cout << [](int n) { return n + 1 ; } (99) << std::endl ;
Related videos on Youtube
Littlebitter
Updated on December 07, 2020Comments
-
Littlebitter over 3 years
I used lambda function to pass it to
std::condition_variable
wait() function, but that is not the case. I use lambda functions that don't receive any parameters, and everything is absolutely clear for me. But I totally don't understand how is used lamdba function that have parameters list. Show lambda with parameters are used? How to pass parameters to them?-
chris about 11 yearsAny resource you find on lambdas will explain how to make them take parameters.
-
Littlebitter about 11 years@chris I know hpw to make then take parameters - [](int parameter) {lambda body}, but show will I use this lambda?
-
chris about 11 yearsJust like a function.
-
Littlebitter about 11 years@chris I was confused that lamdba has no name, answers below helped me
-
-
Littlebitter about 11 yearsahha! and here auto hides int(*p)(int, int)?
-
Admin about 11 years@Littlebitter the type of lambdas is implementation-defined, hence you need
auto
. You can convert some lambdas (those that don't capture anything) to function pointers, but that's not their actual type (similar to how you can convert anint
to along
, but anint
is not along
). -
Littlebitter about 11 yearsand if lambda will be [] (int a, int b) -> int { return a + b; }; it will be possible to create pointer to function variable with type I described, without auto?
-
chris about 11 yearsI'm pretty sure all lambdas are storable in
std::function
, aren't they? And @Littlebitter, Assigning that to the type in your first comment will work because it doesn't capture anything. -
Admin about 11 yearsYes, you can do
int (*lambda)(int, int) = [] (int a, int b) { return a + b; }
, since the lambda doesn't capture any variables.