Skip to content

Instantly share code, notes, and snippets.

@nickav
Created July 14, 2023 15:18
Show Gist options
  • Save nickav/c677d7fdfdce1279c3d5a0f8cd06bd09 to your computer and use it in GitHub Desktop.
Save nickav/c677d7fdfdce1279c3d5a0f8cd06bd09 to your computer and use it in GitHub Desktop.
defer macro thing
#ifndef defer
// Defer macro/thing.
template<typename T>
struct ExitScope {
T lambda;
ExitScope(T lambda) : lambda(lambda) {}
~ExitScope() { lambda(); }
ExitScope(const ExitScope&);
private:
ExitScope& operator =(const ExitScope&);
};
class ExitScopeHelp {
public:
template<typename T>
ExitScope<T> operator+(T t) { return t; }
};
#define defer const auto& CONCAT(defer__, __LINE__) = ExitScopeHelp() + [&]()
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment