in C Plus Plus by
Q:
Lambda capture expressions C++ 14 Feature

1 Answer

0 votes
by
C++11 lambda functions capture variables declared in their outer scope by value-copy or by reference. This means that value members of a lambda cannot be move-only types. C++14 allows captured members to be initialized with arbitrary expressions. This allows both capture by value-move and declaring arbitrary members of the lambda, without having a correspondingly named variable in an outer scope.[7]

This is done via the use of an initializer expression:

auto lambda = [value = 1] {return value;};

The lambda function lambda returns 1, which is what value was initialized with. The declared capture deduces the type from the initializer expression as if by auto.

This can be used to capture by move, via the use of the standard std::move function:

std::unique_ptr<int> ptr(new int(10));

auto lambda = [value = std::move(ptr)] {return *value;};
Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

+1 vote
+1 vote
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
0 votes
asked Oct 31, 2019 in Other by MBarbieri
+2 votes
asked Jan 21 in C Plus Plus by SakshiSharma
+1 vote
asked Jan 20 in C Plus Plus by SakshiSharma
...