Lambda C++14 capture


Kedour

I have come across symbols like this:

int x = 4;
auto y = [&r = x, x = x+1]()->int { 
    r += 2;
    return x+2;
}();

Can you explain this statement? I'm a C++03 user and recently upgraded to C++11. As of today, I started using C++14 and came across this snippet.

Thanks!

Kedour

Thanks to @chris for the wikipedia reference . What I found is-

This is a good explanation, they don't understand the old Lambda C++11 captures

In C++14:


C++11 lambda functions capture variables declared in their outer scope by copy by value or by reference. This means that the value member of a lambda cannot be a move-only type. C++14 allows arbitrary expressions to initialize captured members. This allows capturing and declaring arbitrary members of a lambda by move-by-value without having a correspondingly named variable in the outer scope.

This is done by using initializer expressions:

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

The lambda function lambdawill return 1, which is what is valueinitialized. A declared capture deduces the type from the initializer expression, just like by auto.

It can be used for mobile capture by using standard std::movefunctions :

std::unique_ptr<int> ptr(new int(10));
auto lambda = [value = std::move(ptr)] {return *value;};

So the above expression updates x to 6 and initializes y to 7.

Related


Lambda C++14 capture

Kedour I have come across symbols like this: int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); Can you explain this statement? I'm a C++03 user and recently upgraded to C++11. As of today, I started using C++14 and came across t

Capture and move unique_ptr in C++14 Lambda expression

Martin Mozart I capture unique_ptr in lambda expression this way: auto str = make_unique<string>("my string"); auto lambda = [ capturedStr = std::move(str) ] { cout << *capturedStr.get() << endl; }; lambda(); It worked fine until I tried to move capturedSt

c++14 Variadic lambda capture for function binding

and h I'm currently reading several books to learn about c++14 features. I'm trying to bind parameters to a function using variadic templates. I know how to use std::bind to do this, but I would also like to use c++14 lambda expressions to do this, just out of

Capture and move unique_ptr in C++14 Lambda expression

Martin Mozart I capture unique_ptr in lambda expression this way: auto str = make_unique<string>("my string"); auto lambda = [ capturedStr = std::move(str) ] { cout << *capturedStr.get() << endl; }; lambda(); It worked fine until I tried to move capturedSt

C++ Lambda code generation with Init Capture in C++14

Blair Davidson I'm trying to understand/clarify the code generated when passing captures to lambdas, especially in the generalized init captures added in C++14. Given the following code samples listed below, this is my current understanding of what the compile

About initialization capture of C++14 lambda

username Why update the value x to 6 and initialize y to 7? Can someone give me some clues? int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); // Updates ::x to 6, and initializes y to 7. Tzu Chi In a cap

About initialization capture of C++14 lambda

username Why update the value x to 6 and initialize y to 7? Can someone give me some clues? int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); // Updates ::x to 6, and initializes y to 7. Tzu Chi In a cap

c++14 Variadic lambda capture for function binding

and h I'm currently reading several books to learn about c++14 features. I'm trying to bind parameters to a function using variadic templates. I know how to use std::bind to do this, but I would also like to use c++14 lambda expressions to do this, just out of

About initialization capture of C++14 lambda

username Why update the value x to 6 and initialize y to 7? Can someone give me some clues? int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); // Updates ::x to 6, and initializes y to 7. Tzu Chi In a cap

Capture and move unique_ptr in C++14 Lambda expression

Martin Mozart I capture unique_ptr in lambda expression this way: auto str = make_unique<string>("my string"); auto lambda = [ capturedStr = std::move(str) ] { cout << *capturedStr.get() << endl; }; lambda(); It worked fine until I tried to move capturedSt

c++14 Variadic lambda capture for function binding

and h I'm currently reading several books to learn about c++14 features. I'm trying to bind parameters to a function using variadic templates. I know how to use std::bind to do this, but I would also like to use c++14 lambda expressions to do this, just out of

Lambda C++14 capture

Kedour I have come across symbols like this: int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); Can you explain this statement? I'm a C++03 user and recently upgraded to C++11. As of today, I started using C++14 and came across t

C++ Lambda code generation with Init Capture in C++14

Blair Davidson I'm trying to understand/clarify the code generated when passing captures to lambdas, especially in the generalized init captures added in C++14. Given the following code samples listed below, this is my current understanding of what the compile

About initialization capture of C++14 lambda

username Why update the value x to 6 and initialize y to 7? Can someone give me some clues? int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); // Updates ::x to 6, and initializes y to 7. Tzu Chi In a cap

Lambda C++14 capture

Kedour I have come across symbols like this: int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); Can you explain this statement? I'm a C++03 user and recently upgraded to C++11. As of today, I started using C++14 and came across t

Capture and move unique_ptr in C++14 Lambda expression

Martin Mozart I capture unique_ptr in lambda expression this way: auto str = make_unique<string>("my string"); auto lambda = [ capturedStr = std::move(str) ] { cout << *capturedStr.get() << endl; }; lambda(); It worked fine until I tried to move capturedSt

C++ Lambda code generation with Init Capture in C++14

Blair Davidson I'm trying to understand/clarify the code generated when passing captures to lambdas, especially in the generalized init captures added in C++14. Given the following code samples listed below, this is my current understanding of what the compile

About initialization capture of C++14 lambda

username Why update the value x to 6 and initialize y to 7? Can someone give me some clues? int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); // Updates ::x to 6, and initializes y to 7. Tzu Chi In a cap

About initialization capture of C++14 lambda

username Why update the value x to 6 and initialize y to 7? Can someone give me some clues? int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); // Updates ::x to 6, and initializes y to 7. Tzu Chi In a cap

Capture and move unique_ptr in C++14 Lambda expression

Martin Mozart I capture unique_ptr in lambda expression this way: auto str = make_unique<string>("my string"); auto lambda = [ capturedStr = std::move(str) ] { cout << *capturedStr.get() << endl; }; lambda(); It worked fine until I tried to move capturedSt

c++14 Variadic lambda capture for function binding

and h I'm currently reading several books to learn about c++14 features. I'm trying to bind parameters to a function using variadic templates. I know how to use std::bind to do this, but I would also like to use c++14 lambda expressions to do this, just out of

c++14 Variadic lambda capture for function binding

and h I'm currently reading several books to learn about c++14 features. I'm trying to bind parameters to a function using variadic templates. I know how to use std::bind to do this, but I would also like to use c++14 lambda expressions to do this, just out of

C++ Lambda code generation with Init Capture in C++14

Blair Davidson I'm trying to understand/clarify the code generated when passing captures to lambdas, especially in the generalized init captures added in C++14. Given the following code samples listed below, this is my current understanding of what the compile

C++ Lambda code generation with Init Capture in C++14

Blair Davidson I'm trying to understand/clarify the code generated when passing captures to lambdas, especially in the generalized init captures added in C++14. Given the following code samples listed below, this is my current understanding of what the compile

About initialization capture of C++14 lambda

username Why update the value x to 6 and initialize y to 7? Can someone give me some clues? int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); // Updates ::x to 6, and initializes y to 7. Tzu Chi In a cap

About initialization capture of C++14 lambda

username Why update the value x to 6 and initialize y to 7? Can someone give me some clues? int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); // Updates ::x to 6, and initializes y to 7. Tzu Chi In a cap