Skip to content

Instantly share code, notes, and snippets.

@oteguro
Created May 8, 2014 13:33
Show Gist options
  • Save oteguro/c8ec15b4806a5c75eae7 to your computer and use it in GitHub Desktop.
Save oteguro/c8ec15b4806a5c75eae7 to your computer and use it in GitHub Desktop.
Tiny "ScopeExit" macro on C++11.
// ----------------------------------------------------------------------------
// scope_exit.h
// ----------------------------------------------------------------------------
// Description : Tiny "ScopeExit" on C++11.
#pragma once
template <typename T> class ScopeExit
{
public:
T m_func;
ScopeExit(T func)
: m_func(func)
{
}
~ScopeExit()
{
m_func();
}
}; // class ScopeExit
template <typename T> ScopeExit<T> MakeScopeExit(T func)
{
return ScopeExit<T>(func);
}
#define SCOPE_EXIT_CONCAT_IMPL(x,y) x ## y
#define SCOPE_EXIT_CONCAT(x,y) SCOPE_EXIT_CONCAT_IMPL(x,y)
#define SCOPE_EXIT(code) auto SCOPE_EXIT_CONCAT(scopeExit, __LINE__) = MakeScopeExit([&](){code;})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment