fork(3) download
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5. #define def_unnamed_imp(x, y) unnamed_##x##_##y
  6. #define def_unnamed_decl(X, Y) def_unnamed_imp(X, Y)
  7. #define UNNAMED def_unnamed_decl(__COUNTER__, __LINE__)
  8.  
  9. class call_at_delete
  10. {
  11. std::function<void()> m_functor;
  12. public:
  13. template<class Functor>
  14. call_at_delete(const Functor& _func) : m_functor(_func){}
  15. ~call_at_delete(){m_functor();}
  16. };
  17.  
  18. #define finally call_at_delete UNNAMED = [&]()
  19.  
  20.  
  21. void test()
  22. {
  23. int i = 10;
  24.  
  25. finally
  26. {
  27. cout << i << " hello\n";
  28. };
  29. try
  30. {
  31. i = 100;
  32. throw exception();
  33. }
  34. catch(...)
  35. {
  36. cout << "exeption\n";
  37. }
  38.  
  39. }
  40.  
  41.  
  42.  
  43. int main()
  44. {
  45. test();
  46.  
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
exeption
100 hello