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/config.hpp> 10 #ifdef BOOST_NO_CXX11_VARIADIC_MACROS 11 # error "variadic macros required" 12 #else 13 14 #include <boost/scope_exit.hpp> 15 #include <boost/typeof/typeof.hpp> 16 #include <boost/typeof/std/vector.hpp> 17 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() 18 #include <boost/detail/lightweight_test.hpp> 19 #include <boost/config.hpp> 20 #include <vector> 21 22 struct person {}; 23 BOOST_TYPEOF_REGISTER_TYPE(person) 24 25 struct world { 26 void add_person(person const& a_person); populationworld27 size_t population(void) const { return persons_.size(); } 28 29 private: 30 std::vector<person> persons_; 31 }; BOOST_TYPEOF_REGISTER_TYPE(world)32BOOST_TYPEOF_REGISTER_TYPE(world) 33 34 void world::add_person(person const& a_person) { 35 bool commit = false; 36 37 persons_.push_back(a_person); 38 //[world_this 39 BOOST_SCOPE_EXIT(&commit, this_) { // Capture object `this_`. 40 if(!commit) this_->persons_.pop_back(); 41 } BOOST_SCOPE_EXIT_END 42 //] 43 44 // ... 45 46 commit = true; 47 } 48 main(void)49int main(void) { 50 world w; 51 person p; 52 w.add_person(p); 53 BOOST_TEST(w.population() == 1); 54 return boost::report_errors(); 55 } 56 57 #endif // variadic macros 58 59