scope_guard_seq.cpp 939 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (C) 2006-2009, 2012 Alexander Nasonov
  2. // Copyright (C) 2012 Lorenzo Caminiti
  3. // Distributed under the Boost Software License, Version 1.0
  4. // (see accompanying file LICENSE_1_0.txt or a copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Home at http://www.boost.org/libs/scope_exit
  7. #include <boost/scope_exit.hpp>
  8. #include <boost/typeof/std/string.hpp>
  9. #include <boost/typeof/std/map.hpp>
  10. #include <map>
  11. #include <string>
  12. #include <utility>
  13. int main(void) {
  14. bool commit = false;
  15. std::string currency("EUR");
  16. double rate = 1.3326;
  17. std::map<std::string, double> rates;
  18. bool currency_rate_inserted =
  19. rates.insert(std::make_pair(currency, rate)).second;
  20. BOOST_SCOPE_EXIT( (currency_rate_inserted) (&commit) (&rates)
  21. (&currency) ) {
  22. if(currency_rate_inserted && !commit) rates.erase(currency);
  23. } BOOST_SCOPE_EXIT_END
  24. // ...
  25. commit = true;
  26. return 0;
  27. }