Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <utility>
- #include <iostream>
- #include <exception>
- template <typename Functor>
- class Defer {
- public:
- Defer(Functor&& functor) : functor_(std::forward<Functor>(functor)) {}
- Defer(const Defer&) = delete;
- Defer(Defer&&) = delete;
- ~Defer() {
- try {
- functor_();
- } catch (...) {
- std::cerr << "DO NOT throw exception in defer function" << std::endl;
- std::terminate();
- }
- }
- private:
- Functor functor_;
- };
- template <typename Functor>
- Defer<Functor> make_defer(Functor&& functor) {
- return Defer<Functor>(std::forward<Functor>(functor));
- }
Add Comment
Please, Sign In to add comment