Advertisement
Caminhoneiro

Inside the lambda with pointer

Jul 3rd, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5.  //Will give error
  6. int main() {
  7.     auto p = make_unique<int>(64);
  8.     auto lambda = []() { //To access some variable declared in the external scope like the p must use the lambda capture list inside the brackets
  9.         cout << " Inside the lambda -- value = " << *p << '\n';
  10.     };
  11.  
  12.     lambda();
  13. }
  14.  
  15. //Will work
  16. int main() {
  17.     auto p = make_unique<int>(64);
  18.     auto lambda = [ptr = move(p)]() {
  19.         cout << " Inside the lambda -- value = " << *ptr << '\n';
  20.     };
  21.  
  22.     lambda();
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement