#ifndef __DEFER__
template <class F>
struct defer_caller {
F f;
~defer_caller() { f(); }
};
struct defer_dummy {};
template <class F>
defer_caller<F> operator*(defer_dummy, F f) {
return { f };
}
#define defer_varname_(NUM) zz_defer_##NUM
#define defer_varname(NUM) defer_varname_(NUM)
#define defer auto defer_varname(__COUNTER__) = defer_dummy{} * [&]()
// ex)
// FILE* topSecret = fopen("cia.txt");
// defer{ fclose(topSecret); };
#endif // !__DEFER__
https://blukat.me/2021/02/golang-like-defer-in-cpp/
https://stackoverflow.com/a/42060129/8939955
반응형
'프로그래밍 > C,C++' 카테고리의 다른 글
함수(function) (0) | 2015.03.24 |
---|---|
상속의 기능 (0) | 2015.02.14 |