fork download
  1. #include <functional>
  2.  
  3. struct lols {
  4. std::function<void()> mem;
  5. template<typename T> lols(T&& t)
  6. : mem(t) {}
  7. lols(lols&& other) { mem.swap(other.mem); }
  8. ~lols() { if (mem) mem(); }
  9. };
  10.  
  11. template<typename T> lols finally(T&& t) {
  12. return lols { std::forward<T>(t) };
  13. }
  14.  
  15. #include <iostream>
  16. int print(int i)
  17. {
  18. std::cout << i << ' ';
  19. return i;
  20. }
  21.  
  22. int main() {
  23. auto f = finally([] { print(13); });
  24. return print(0);
  25. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
0 13