world_void.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/typeof.hpp>
  9. #include <boost/typeof/std/vector.hpp>
  10. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <vector>
  13. struct person {};
  14. BOOST_TYPEOF_REGISTER_TYPE(person)
  15. struct world_t;
  16. BOOST_TYPEOF_REGISTER_TYPE(world_t)
  17. //[world_void
  18. struct world_t {
  19. std::vector<person> persons;
  20. bool commit;
  21. } world; // Global variable.
  22. void add_person(person const& a_person) {
  23. world.commit = false;
  24. world.persons.push_back(a_person);
  25. BOOST_SCOPE_EXIT(void) { // No captures.
  26. if(!world.commit) world.persons.pop_back();
  27. } BOOST_SCOPE_EXIT_END
  28. // ...
  29. world.commit = true;
  30. }
  31. //]
  32. int main(void) {
  33. person p;
  34. add_person(p);
  35. BOOST_TEST(world.persons.size() == 1);
  36. return boost::report_errors();
  37. }