Created
February 14, 2019 20:38
-
-
Save MaZderMind/adfc750b533fa10c5a236729aaa2adf8 to your computer and use it in GitHub Desktop.
cpp_scope_guards.cpp
This file contains hidden or 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> | |
// 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