Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. template<class FuncT>
  2. struct final_action {
  3.   explicit final_action(FuncT func) :
  4.     final_function{std::move(func)} {
  5.   }
  6.  
  7.   final_action(final_action &&other) = delete;
  8.   final_action(const final_action &) = delete;
  9.   final_action &operator=(final_action &&other) = delete;
  10.   final_action &operator=(const final_action &) = delete;
  11.  
  12.   ~final_action() {
  13.     final_function();
  14.   }
  15.  
  16. private:
  17.   FuncT final_function;
  18. };
  19.  
  20. template<class FuncT>
  21. final_action<FuncT> finally(FuncT &&final_function) {
  22.   return final_action<FuncT>{std::forward<FuncT>(final_function)};
  23. }
  24.  
  25. int foo() {
  26.     audo defer = finally([]() { // some code at the end of function});
  27.     // function code
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement