#include #include #include #include #include #include #include namespace boost { namespace scope_exit { struct guard { template guard(Lambda f) : f_(f) {} ~guard() { f_(); } private: boost::function f_; }; } } // namespace void close_handle(int& h) { std::cout << "closing: " << h << std::endl; h = 0; } void close_handle_unless(bool const cond, int& h) { if(!cond) close_handle(h); } BOOST_PHOENIX_ADAPT_FUNCTION(void, close_handle_, close_handle, 1) int main() { bool dismiss = false; int x = 1; BOOST_SCOPE_EXIT(&dismiss, &x) { if(!dismiss) close_handle(x); } BOOST_SCOPE_EXIT_END #ifndef BOOST_NO_LAMBDAS int y = 2; boost::scope_exit::guard exit_y = [&dismiss, &y] { if(!dismiss) close_handle(y); }; #endif int z = 3; boost::scope_exit::guard exit_z = boost::bind( close_handle_unless, boost::ref(dismiss), boost::ref(z)); using namespace boost::phoenix; using namespace boost::phoenix::placeholders; int w = 4; boost::scope_exit::guard exit_w = if_(!ref(dismiss)) [ close_handle_(ref(w)) ] ; // dismiss = true; return 0; }