Guest User

Untitled

a guest
Nov 9th, 2019
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. #define CONNECTION(text1,text2) text1##text2
  5. #define CONNECT(text1,text2) CONNECTION(text1,text2)
  6.  
  7. #define defer(command) DeferHelper CONNECT(DEFER, __LINE__)([](){command;})
  8.  
  9. typedef void (*Defer)();
  10.  
  11. class DeferHelper {
  12.  
  13. Defer defer_func;
  14.  
  15. public:
  16. ~DeferHelper() {
  17. defer_func();
  18. }
  19.  
  20. public:
  21. DeferHelper(Defer f) {
  22. defer_func = f;
  23. }
  24. };
  25.  
  26.  
  27.  
  28. int main() {
  29.  
  30. defer(std::cout << "defer1" <<std::endl);
  31. defer(std::cout << "defer2" <<std::endl);
  32.  
  33. std::cout << "method" << std::endl;
  34.  
  35. return 0;
  36. }
  37. /**
  38. int main() {
  39.  
  40. DeferHelper DEFER29([](){std::cout << "defer" <<std::endl;});
  41. DeferHelper DEFER30([](){std::cout << "defer" <<std::endl;});
  42.  
  43. std::cout << "method" << std::endl;
  44.  
  45. return 0;
  46. }
  47. */
RAW Paste Data