1 2 // Copyright (C) 2006-2009, 2012 Alexander Nasonov 3 // Copyright (C) 2012 Lorenzo Caminiti 4 // Distributed under the Boost Software License, Version 1.0 5 // (see accompanying file LICENSE_1_0.txt or a copy at 6 // http://www.boost.org/LICENSE_1_0.txt) 7 // Home at http://www.boost.org/libs/scope_exit 8 9 #include <boost/scope_exit.hpp> 10 #include <boost/typeof/typeof.hpp> 11 #include <boost/typeof/std/vector.hpp> 12 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() 13 #include <boost/detail/lightweight_test.hpp> 14 #include <vector> 15 16 struct person {}; 17 BOOST_TYPEOF_REGISTER_TYPE(person) 18 19 struct world_t; 20 BOOST_TYPEOF_REGISTER_TYPE(world_t) 21 22 //[world_void 23 struct world_t { 24 std::vector<person> persons; 25 bool commit; 26 } world; // Global variable. 27 add_person(person const & a_person)28void add_person(person const& a_person) { 29 world.commit = false; 30 world.persons.push_back(a_person); 31 32 BOOST_SCOPE_EXIT(void) { // No captures. 33 if(!world.commit) world.persons.pop_back(); 34 } BOOST_SCOPE_EXIT_END 35 36 // ... 37 38 world.commit = true; 39 } 40 //] 41 main(void)42int main(void) { 43 person p; 44 add_person(p); 45 BOOST_TEST(world.persons.size() == 1); 46 return boost::report_errors(); 47 } 48 49