// Copyright (C) 2006-2009, 2012 Alexander Nasonov // Copyright (C) 2012 Lorenzo Caminiti // Distributed under the Boost Software License, Version 1.0 // (see accompanying file LICENSE_1_0.txt or a copy at // http://www.boost.org/LICENSE_1_0.txt) // Home at http://www.boost.org/libs/scope_exit #include #ifndef BOOST_NO_LAMBDAS #define BOOST_TEST_MODULE TestWorldLambda #include #include struct person {}; struct world { void add_person(person const& a_person); std::vector persons_; }; //[world_lambda #include struct scope_exit { scope_exit(std::function f) : f_(f) {} ~scope_exit(void) { f_(); } private: std::function f_; }; void world::add_person(person const& a_person) { bool commit = false; persons_.push_back(a_person); scope_exit on_exit1([&commit, &persons_](void) { // Use C++11 lambda. if(!commit) persons_.pop_back(); }); // ... commit = true; } //] BOOST_AUTO_TEST_CASE( test_world_lambda ) { world w; person p; w.add_person(p); BOOST_CHECK( w.persons_.size() == 1 ); } #else int main(void) { return 0; } // Trivial test. #endif