Skip to content

Instantly share code, notes, and snippets.

@stillwwater
Last active August 21, 2022 01:38
Show Gist options
  • Save stillwwater/1f6db2e977e1d04ed8ec97c64f4dbfb6 to your computer and use it in GitHub Desktop.
Save stillwwater/1f6db2e977e1d04ed8ec97c64f4dbfb6 to your computer and use it in GitHub Desktop.
#define DEFER_CAT_(A, B) A ## B ## _
#define DEFER_CAT(A, B) DEFER_CAT_(A, B)
template <typename Func>
struct Defer {
Func fn;
Defer(Func fn_) : fn(fn_) {}
~Defer() { fn(); }
};
// Executes expression at the end of the enclosing scope.
#define defer(expr) auto DEFER_CAT(defer_exp_, __LINE__) = Defer([&] { expr; })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment