Last active
August 21, 2022 01:38
-
-
Save stillwwater/1f6db2e977e1d04ed8ec97c64f4dbfb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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