Skip to content

Instantly share code, notes, and snippets.

@MaZderMind
Created February 14, 2019 20:38
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 MaZderMind/adfc750b533fa10c5a236729aaa2adf8 to your computer and use it in GitHub Desktop.
Save MaZderMind/adfc750b533fa10c5a236729aaa2adf8 to your computer and use it in GitHub Desktop.
cpp_scope_guards.cpp
#include <iostream>
// https://github.com/ricab/scope_guard
#include "scope_guard.hpp"
int main (int argc, char** argv)
{
std::cout << "enter main" << std::endl;
std::cout << "open something" << std::endl;
auto something_guard = sg::make_scope_guard([]{
std::cout << "close something" << std::endl;
});
{
std::cout << "opening some file" << std::endl;
auto file_guard = sg::make_scope_guard([]{
std::cout << "closing the file" << std::endl;
});
// return 1; // the file and "something" is cleaned up
std::cout << "work with the file" << std::endl;
std::cout << "end of the block" << std::endl;
} // the file is closed here
// return 2; // "something" is cleaned up
std::cout << "open something else" << std::endl;
auto something_else_guard = sg::make_scope_guard([]{
std::cout << "close something else" << std::endl;
});
return 0; // "something" and "something else" is cleaned up here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment