typedef std::function<void(void)> DeferFunc; class Defer { private: DeferFunc func; public: explicit Defer(DeferFunc func) : func{func} {}; ~Defer() { func(); func = nullptr; }; }; int main() { .... SDL_Window* window = SDL_CreateWindow("EuX Force", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN); if (window == nullptr) { cerr << "Create window failed" << SDL_GetError() << endl; return EXIT_FAILURE; } Defer clear_window([window]() { SDL_DestroyWindow(window); cout << "window.free" << endl; }); return EXIT_SUCCESS: }