I tried to increment a local variable from lambda expression:
#include <iostream>
template<typename T>
T foo(T t){
T temp{};
[temp]() -> void {
temp++;
}();
return temp;
}
int main()
{
std::cout<< foo(10) << std::endl;
}
DEMO
But got the following error:
main.cpp: In instantiation of 'foo(T)::<lambda()> [with T = int]':
main.cpp:6:6: required from 'struct foo(T) [with T = int]::<lambda()>'
main.cpp:8:6: required from 'T foo(T) [with T = int]'
main.cpp:14:23: required from here
main.cpp:7:13: error: increment of read-only variable 'temp'
temp++;
^
Is there some workaround about that in c++11/14?
If you really need to increment a counter from within a lambda, the typical way to do so is to make the counter an AtomicInteger or AtomicLong and then call one of the increment methods on it. You could use a single-element int or long array, but that would have race conditions if the stream is run in parallel.
Yes, you can modify local variables from inside lambdas (in the way shown by the other answers), but you should not do it.
Because the local variables declared outside the lambda expression can be final or effectively final. The rule of final or effectively final is also applicable for method parameters and exception parameters. The this and super references inside a lambda expression body are the same as their enclosing scope.
C++ Lambda expression allows us to define anonymous function objects (functors) which can either be used inline or passed as an argument. Lambda expression was introduced in C++11 for creating anonymous functors in a more convenient and concise way.
temp
cannot be modified when it's captured by copy in a non-mutable lambda.
You could capture temp
by reference:
template<typename T>
T foo(T t){
T temp{};
[&temp]() -> void {
temp++;
}();
return temp;
}
If you want to capture a variable by value and modify it, you need to mark the lambda mutable
:
[temp]() mutable -> void {
// ^^^^^^^
temp++;
}();
This allows the body to modify the parameters captured by value, and to call their non-const member functions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With