Created
January 6, 2016 05:03
-
-
Save cho45/73cd3672cfe38cce6ddd to your computer and use it in GitHub Desktop.
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
#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