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 macro 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 <vector> 20 21 struct person {}; 22 BOOST_TYPEOF_REGISTER_TYPE(person) 23 24 template<typename Person> 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 }; 32 BOOST_TYPEOF_REGISTER_TEMPLATE(world, 1) 33 34 //[world_tpl 35 template<typename Person> add_person(Person const & a_person)36void world<Person>::add_person(Person const& a_person) { 37 bool commit = false; 38 persons_.push_back(a_person); 39 40 BOOST_SCOPE_EXIT_TPL(&commit, this_) { // Use `_TPL` postfix. 41 if(!commit) this_->persons_.pop_back(); 42 } BOOST_SCOPE_EXIT_END 43 44 // ... 45 46 commit = true; 47 } 48 //] 49 main(void)50int main(void) { 51 world<person> w; 52 person p; 53 w.add_person(p); 54 BOOST_TEST(w.population() == 1); 55 return boost::report_errors(); 56 } 57 58 #endif // variadic macros 59 60