fork(1) download
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5. struct Block {
  6. function<int()> callback;
  7. };
  8.  
  9. void set_callback(Block &block) {
  10. int x = 1;
  11. int y = 2;
  12.  
  13. block.callback = [x=std::move(x), y=std::move(y)] {
  14. return x + y;
  15. };
  16. }
  17.  
  18. void run_callback(const Block &block) {
  19. cout << "result: " << block.callback() << endl;
  20. }
  21.  
  22. int main() {
  23.  
  24. Block block;
  25.  
  26. set_callback(block);
  27.  
  28. run_callback(block);
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
result: 3