3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Chapter 1. Boost.ScopeExit 1.1.0</title>
5 <link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="index.html" title="Chapter 1. Boost.ScopeExit 1.1.0">
8 <link rel="next" href="scope_exit/getting_started.html" title="Getting Started">
10 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
11 <table cellpadding="2" width="100%"><tr>
12 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
13 <td align="center"><a href="../../../../index.html">Home</a></td>
14 <td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
15 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
16 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
17 <td align="center"><a href="../../../../more/index.htm">More</a></td>
20 <div class="spirit-nav"><a accesskey="n" href="scope_exit/getting_started.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a></div>
22 <div class="titlepage"><div>
23 <div><h2 class="title">
24 <a name="scope_exit"></a>Chapter 1. Boost.ScopeExit 1.1.0</h2></div>
25 <div><div class="author"><h3 class="author">
26 <span class="firstname">Alexander</span> <span class="surname">Nasonov</span>
28 <div><div class="author"><h3 class="author">
29 <span class="firstname">Lorenzo</span> <span class="surname">Caminiti <code class="email"><<a class="email" href="mailto:lorcaminiti@gmail.com">lorcaminiti@gmail.com</a>></code></span>
31 <div><p class="copyright">Copyright © 2006-2012 Alexander Nasonov, Lorenzo Caminiti</p></div>
32 <div><div class="legalnotice">
33 <a name="scope_exit.legal"></a><p>
34 Distributed under the Boost Software License, Version 1.0 (see accompanying
35 file LICENSE_1_0.txt or a copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
40 <p><b>Table of Contents</b></p>
42 <dt><span class="section"><a href="index.html#scope_exit.introduction">Introduction</a></span></dt>
43 <dt><span class="section"><a href="scope_exit/getting_started.html">Getting Started</a></span></dt>
45 <dt><span class="section"><a href="scope_exit/getting_started.html#scope_exit.getting_started.this_documentation">This Documentation</a></span></dt>
46 <dt><span class="section"><a href="scope_exit/getting_started.html#scope_exit.getting_started.compilers_and_platforms">Compilers
47 and Platforms</a></span></dt>
48 <dt><span class="section"><a href="scope_exit/getting_started.html#scope_exit.getting_started.installation">Installation</a></span></dt>
50 <dt><span class="section"><a href="scope_exit/tutorial.html">Tutorial</a></span></dt>
52 <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.capturing_variables">Capturing Variables</a></span></dt>
53 <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.capturing_the_object__this_">Capturing
54 The Object <code class="computeroutput"><span class="keyword">this</span></code></a></span></dt>
55 <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.capturing_no_variable">Capturing
56 No Variable</a></span></dt>
57 <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.capturing_all_variables__c__11_only_">Capturing
58 All Variables (C++11 Only)</a></span></dt>
59 <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.template_workaround__gcc_">Template
60 Workaround (GCC)</a></span></dt>
61 <dt><span class="section"><a href="scope_exit/tutorial.html#scope_exit.tutorial.same_line_expansions">Same Line Expansions</a></span></dt>
63 <dt><span class="section"><a href="scope_exit/alternatives.html">Annex: Alternatives</a></span></dt>
64 <dt><span class="section"><a href="scope_exit/no_variadic_macros.html">Annex: No Variadic Macros</a></span></dt>
65 <dt><span class="section"><a href="reference.html">Reference</a></span></dt>
66 <dt><span class="section"><a href="scope_exit/acknowledgements.html">Acknowledgements</a></span></dt>
70 This library allows to execute arbitrary code when the enclosing scope exits.
73 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
74 <a name="scope_exit.introduction"></a><a class="link" href="index.html#scope_exit.introduction" title="Introduction">Introduction</a>
75 </h2></div></div></div>
77 Nowadays, every C++ developer is familiar with the Resource Acquisition Is
78 Initialization (<a href="http://www.research.att.com/~bs/glossary.html#Gresource-acquisition-is-initialization" target="_top">RAII</a>)
79 technique. It binds resource acquisition and release to initialization and
80 destruction of a variable that holds the resource. There are times when writing
81 a special class for such a variable is not worth the effort. This is when
82 <a class="link" href="index.html" title="Chapter 1. Boost.ScopeExit 1.1.0">Boost.ScopeExit</a> comes into play.
85 Programmers can put resource acquisition directly in their code and next to
86 it, they can write code that releases the resource using this library. For
87 example (see also <a href="../../test/world.cpp" target="_top"><code class="literal">world.cpp</code></a>):
88 <a href="#ftn.scope_exit.introduction.f0" class="footnote" name="scope_exit.introduction.f0"><sup class="footnote">[1]</sup></a>
92 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">world</span><span class="special">::</span><span class="identifier">add_person</span><span class="special">(</span><span class="identifier">person</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">a_person</span><span class="special">)</span> <span class="special">{</span>
93 <span class="keyword">bool</span> <span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span>
95 <span class="identifier">persons_</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">a_person</span><span class="special">);</span> <span class="comment">// (1) direct action</span>
96 <span class="comment">// Following block is executed when the enclosing scope exits.</span>
97 <span class="identifier">BOOST_SCOPE_EXIT</span><span class="special">(&</span><span class="identifier">commit</span><span class="special">,</span> <span class="special">&</span><span class="identifier">persons_</span><span class="special">)</span> <span class="special">{</span>
98 <span class="keyword">if</span><span class="special">(!</span><span class="identifier">commit</span><span class="special">)</span> <span class="identifier">persons_</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span> <span class="comment">// (2) rollback action</span>
99 <span class="special">}</span> <span class="identifier">BOOST_SCOPE_EXIT_END</span>
101 <span class="comment">// ... // (3) other operations</span>
103 <span class="identifier">commit</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span> <span class="comment">// (4) disable rollback actions</span>
104 <span class="special">}</span>
109 <div class="footnotes">
110 <br><hr style="width:100; text-align:left;margin-left: 0">
111 <div id="ftn.scope_exit.introduction.f0" class="footnote"><p><a href="#scope_exit.introduction.f0" class="para"><sup class="para">[1] </sup></a>
112 Older versions of this library used a <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a>
113 sequence to specify the list of captured variables. While maintaining full
114 backward compatibility, it is now possible to specify the captured variables
115 also using a comma-separated list (which is the preferred syntax). See the
116 <a class="link" href="scope_exit/no_variadic_macros.html" title="Annex: No Variadic Macros">No Variadic Macros</a> section
117 for more information.
121 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
122 <td align="left"><p><small>Last revised: April 17, 2017 at 02:27:50 GMT</small></p></td>
123 <td align="right"><div class="copyright-footer"></div></td>
126 <div class="spirit-nav"><a accesskey="n" href="scope_exit/getting_started.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a></div>