Skip to content

Instantly share code, notes, and snippets.

@cho45
Created January 6, 2016 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cho45/73cd3672cfe38cce6ddd to your computer and use it in GitHub Desktop.
Save cho45/73cd3672cfe38cce6ddd to your computer and use it in GitHub Desktop.
#include <iostream>
// デストラクタで func を実行するだけ
template <class T>
class ScopeGuard {
const T func;
public:
ScopeGuard(T f) : func(f) {}
~ScopeGuard() {
func();
}
};
// XXX: T を明示的に指定せずに ScopeGuard クラスを作るためにはラッパ関数が必要
template <class T>
ScopeGuard<T> scope_guard(T f) {
return ScopeGuard<T>(f);
}
int main() {
auto guard = scope_guard([]{
std::cout << "guard" << std::endl;
});
std::cout << "foobar" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment