fork download
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. class ScopeGuard
  7. {
  8. bool commited;
  9. stringstream accumulator;
  10. public:
  11. ScopeGuard() : commited(false) {}
  12. void commit()
  13. {
  14. commited = true;
  15. }
  16. bool done() const
  17. {
  18. return commited;
  19. }
  20. template<typename Data>
  21. ScopeGuard &operator<<(const Data &x)
  22. {
  23. accumulator << x;
  24. return *this;
  25. }
  26. ~ScopeGuard()
  27. {
  28. if(commited)
  29. cout << accumulator.str();
  30. }
  31. };
  32.  
  33. #define LOG(anything) \
  34. for(ScopeGuard guard; !guard.done(); guard.commit()) \
  35.   guard \
  36. /**/
  37.  
  38.  
  39. int main()
  40. {
  41. try
  42. {
  43. LOG(lg) << "Failure" << (false ? "\n" : throw 1);
  44. }
  45. catch(...) {}
  46.  
  47. LOG(lg) << "Success" << (true ? "\n" : throw 1);
  48. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
Success