fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. namespace Sys {
  5.  
  6. // RANGE BASED Loops
  7. class range {
  8. private:
  9. int first;
  10. int last;
  11. int iter;
  12.  
  13. public:
  14. range(int end):
  15. first(0),
  16. last(end),
  17. iter(0)
  18. {}
  19.  
  20. range(int start, int end) :
  21. first(start),
  22. last(end),
  23. iter(start)
  24. {}
  25.  
  26. // Iterable functions
  27. const range& begin() const { return *this; }
  28. const range& end() const { return *this; }
  29.  
  30. // Iterator functions
  31. bool operator!=(const range&) const { return iter < last; }
  32. void operator++() { ++iter; }
  33. int operator*() const { return iter; }
  34. };
  35.  
  36. // Golang style DEFER
  37. // Usage defer(statement);
  38.  
  39. template <typename F>
  40. struct DeferredAction {
  41. private:
  42. F f;
  43. public:
  44. DeferredAction(F f) : f(f) {}
  45. ~DeferredAction() { f(); }
  46. };
  47.  
  48. template <typename F>
  49. DeferredAction<F> defer_func(F f) {
  50. return DeferredAction<F>(f);
  51. }
  52.  
  53. #define $DEFER_1(x, y) x##y
  54. #define $DEFER_2(x, y) $DEFER_1(x, y)
  55. #define $DEFER_3(x) $DEFER_2(x, __COUNTER__)
  56. #define $defer(code) auto $DEFER_3(_defer_) = ::Sys::defer_func([&](){code;})
  57.  
  58. }
  59.  
  60. int main() {
  61. // your code goes here
  62. $defer(std::cout << "Hello World");
  63. return 0;
  64. }
Success #stdin #stdout 0s 4380KB
stdin
Standard input is empty
stdout
Hello World