Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include <type_traits>
  2. #include <utility>
  3. #include <memory>
  4. #include <iostream>
  5.  
  6. #include <cstdlib>
  7. #include <cassert>
  8.  
  9. namespace
  10. {
  11.  
  12. template< typename lambda >
  13. auto
  14. make_scope_guard(lambda && _lambda)
  15. {
  16. struct lambda_caller
  17. {
  18.  
  19. using pointer = std::decay_t< lambda >;
  20.  
  21. void
  22. operator () (pointer l) const noexcept
  23. {
  24. std::forward< lambda >(l)();
  25. }
  26.  
  27. };
  28. return std::unique_ptr< std::decay_t< lambda >, lambda_caller >(std::forward< lambda >(_lambda));
  29. }
  30.  
  31. }
  32.  
  33. int
  34. main()
  35. {
  36. std::cout << 1 << std::endl;
  37. {
  38. std::cout << 2 << std::endl;
  39. [[gnu::unused]] auto && guard_ = make_scope_guard([&] { std::cout << __PRETTY_FUNCTION__ << std::endl; });
  40. std::cout << 3 << std::endl;
  41. }
  42. std::cout << 5 << std::endl;
  43. return EXIT_SUCCESS;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement