Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++14 increment a value inside of lambda function with capture-specifier

Tags:

c++

lambda

c++14

My task is to write a lambda function which increment a value, but I have to use value = 0 capture-specifier. I'm thinking about the following function:

auto lambda = [value = 0]{return ++value}

When this function is called it has to give an incremented value every time. But I know this implementation is wrong, because it's passed by value. How can I do this in C++14?

like image 806
kbenda Avatar asked Apr 09 '17 16:04

kbenda


People also ask

How do you capture a variable in lambda C++?

Much like functions can change the value of arguments passed by reference, we can also capture variables by reference to allow our lambda to affect the value of the argument. To capture a variable by reference, we prepend an ampersand ( & ) to the variable name in the capture.

What is the correct syntax for lambda expression in C++11?

Lambdas can both capture variables and accept input parameters. A parameter list (lambda declarator in the Standard syntax) is optional and in most aspects resembles the parameter list for a function. auto y = [] (int first, int second) { return first + second; };

How do you pass a lambda function in C++?

Permalink. All the alternatives to passing a lambda by value actually capture a lambda's address, be it by const l-value reference, by non-const l-value reference, by universal reference, or by pointer.

How do you capture a member variable in lambda?

To capture the member variables inside lambda function, capture the “this” pointer by value i.e. std::for_each(vec. begin(), vec. end(), [this](int element){ //.... }

What is a captured variable in lambda function?

Because captured variables are members of the lambda object, their values are persisted across multiple calls to the lambda! Much like functions can change the value of arguments passed by reference, we can also capture variables by reference to allow our lambda to affect the value of the argument.

What is empty capture in lambda expression in C++?

A lambda with empty capture clause [ ] can access only those variable which are local to it. Lambda expression can work only on C++ 11 and after versions. This article is contributed by Utkarsh Trivedi.

What is new in C++ 14 Lambda expressions?

C++ 14 does away with this and allows us to use the keyword auto in the input parameters of the lambda expression. Thus the compilers can now deduce the type of parameters during compile time. So, in our previous example, a lambda expression that would work for both integer and floating point values would be

How to capture external variables from enclosing scope in lambda expression?

A lambda expression can have more power than an ordinary function by having access to variables from the enclosing scope. We can capture external variables from enclosing scope by three ways : A lambda with empty capture clause [ ] can access only those variable which are local to it.


Video Answer


2 Answers

You need to make the lambda mutable:

auto lambda = [value = 0]() mutable {return ++value;};
like image 94
Richard Hodges Avatar answered Nov 02 '22 11:11

Richard Hodges


You don't need a capture:

[]{ static int i=0; return ++i; }

Is all you need.

like image 42
rubenvb Avatar answered Nov 02 '22 13:11

rubenvb