Advertisement
Swiftkill

Googlyeyecode

Aug 23rd, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. struct Receiver {
  5.     void receive(std::function<void()> f) {
  6.         func_ = f;
  7.     }
  8.     std::function<void()> func_;
  9. };
  10.  
  11. void pusher(Receiver& r) {
  12.     auto wrapper=[&](std::function<void()> w) {
  13.         r.receive([w=move(w)]() {
  14.            std::cout << "Before" << std::endl;  
  15.            w();
  16.            std::cout << "After" << std::endl;    
  17.         });
  18.     };
  19.  
  20.  
  21.     wrapper([&]() {
  22.         std::cout << "Original" << std::endl;
  23.     });
  24. }
  25.  
  26. int x= 15;
  27. int main() {
  28.     std::function<void()>  a = [&]() {  std::cout << x << std::endl; };
  29.     a();
  30.     x = 20;
  31.     a();
  32.  
  33.     Receiver r;
  34.     pusher(r);
  35.     r.func_();
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement