Skip to content

Instantly share code, notes, and snippets.

@xiaosuo
Created July 25, 2015 15:30
Show Gist options
  • Save xiaosuo/17ba37a7a884994ebbde to your computer and use it in GitHub Desktop.
Save xiaosuo/17ba37a7a884994ebbde to your computer and use it in GitHub Desktop.
class Defer {
public:
explicit Defer(std::function<void()> func) : func_(func) {}
~Defer() { func_(); }
private:
Defer(const Defer &) = delete;
Defer& operator=(const Defer &) = delete;
std::function<void()> func_;
};
#define ____defer(func, line) Defer __defer_##line(func)
#define __defer(func, line) ____defer(func, line)
#define defer(func) __defer(func, __LINE__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment