-
-
Save oboff/34415b28dabb07daf57553b7d6cd13d9 to your computer and use it in GitHub Desktop.
The definitive defer implementation for C++. Syntax: defer { statements; };
This file contains 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
#ifndef defer | |
struct defer_dummy {}; | |
template <class F> struct deferrer { F f; ~deferrer() { f(); } }; | |
template <class F> deferrer<F> operator*(defer_dummy, F f) { return {f}; } | |
#define DEFER_(LINE) zz_defer##LINE | |
#define DEFER(LINE) DEFER_(LINE) | |
#define defer auto DEFER(__LINE__) = defer_dummy{} *[&]() | |
#endif // defer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/42060129