fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5.  
  6. class DoInDtor
  7. {
  8. public:
  9. typedef function<void()> F;
  10. DoInDtor(F f) : f_(f) {};
  11. ~DoInDtor() { f_(); }
  12. private:
  13. F f_;
  14. };
  15.  
  16. void foo()
  17. {
  18. for(;;)
  19. {
  20. DoInDtor byeSayerCustom([](){ cout << "bye\n"; });
  21. auto cond = true; // could of course also be false sometimes
  22. if (cond)
  23. break;
  24. }
  25. // ...
  26. return;
  27. }
  28.  
  29. int main()
  30. {
  31. foo();
  32. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
bye