Skip to content

Instantly share code, notes, and snippets.

@usagi
Created April 13, 2014 12:50
Show Gist options
  • Select an option

  • Save usagi/10582793 to your computer and use it in GitHub Desktop.

Select an option

Save usagi/10582793 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <boost/scope_exit.hpp>
// test scope 1
auto main() -> int
{
BOOST_SCOPE_EXIT_ALL()
{ std::cerr << "scope 1 exit 1\n"; };
BOOST_SCOPE_EXIT_ALL()
{ std::cerr << "scope 1 exit 2\n"; };
// test scope 2
{
auto s2a = new double(1.23);
BOOST_SCOPE_EXIT_ALL(s2a)
{ std::cerr << "scope 2 exit 1 with delete (=,s2a)\n"; delete s2a; };
auto s2b = 0;
BOOST_SCOPE_EXIT_ALL(&s2b)
{ std::cerr << "scope 2 exit 2 with ++ (&,s2b): " << ++s2b << "\n"; };
BOOST_SCOPE_EXIT_ALL(&s2b)
{ std::cerr << "scope 2 exit 3 with ++ (&,s2b): " << ++s2b << "\n"; };
}
BOOST_SCOPE_EXIT_ALL()
{ std::cerr << "scope 1 exit 3\n"; };
}
scope 2 exit 3 with ++ (&,s2b): 1
scope 2 exit 2 with ++ (&,s2b): 2
scope 2 exit 1 with delete (=,s2a)
scope 1 exit 3
scope 1 exit 2
scope 1 exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment