1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <bits/stdc++.h>
class DoSomeThingWhenExit { public: explicit DoSomeThingWhenExit(std::function<void()> call_back_func) : on_exit_callback(std::move(call_back_func)) {}
~DoSomeThingWhenExit() { on_exit_callback(); }
private: std::function<void()> on_exit_callback; };
#define GUARD_LINENAME_CAT(name, line) name##line #define GUARD_LINENAME(name, line) GUARD_LINENAME_CAT(name, line) #define DO_EXIT(callback) DoSomeThingWhenExit GUARD_LINENAME(EXIT, __LINE__)(callback)
int main() { std::ios::sync_with_stdio(false);
DO_EXIT([&](){ std::cout << 1 << std::endl; }); DO_EXIT([&](){ std::cout << 2 << std::endl; });
return 0; }
|