[DEV] add v1.66.0

This commit is contained in:
2018-01-12 21:47:58 +01:00
parent 87059bb1af
commit a97e9ae7d4
49032 changed files with 7668950 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Acknowledgments</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="prev" href="bibliography.html" title="Bibliography">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="bibliography.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_localfunction.acknowledgments"></a><a class="link" href="acknowledgments.html" title="Acknowledgments">Acknowledgments</a>
</h2></div></div></div>
<p>
This section aims to recognize the contributions of <span class="emphasis"><em>all</em></span>
the different people that participated directly or indirectly to the design
and development of this library.
</p>
<p>
Many thanks to Steven Watanabe and Vicente Botet for suggesting to me to use
<a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a> binding
to <a href="http://lists.boost.org/Archives/boost/2010/04/165149.php" target="_top">emulate
local functions</a>. Many thanks to Alexander Nasonov for clarifying how
<a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a> binding
could be used to implement local functions and for some <a href="http://thread.gmane.org/gmane.comp.lib.boost.devel/168612" target="_top">early
work</a> in this direction.
</p>
<p>
Many thanks to Gregory Crosswhite for using an early version of this library
in <a href="https://github.com/gcross/CodeSearch" target="_top">one of his projects</a>.
</p>
<p>
Thanks to David Abrahams, Vicente Botet, et al. for suggesting to provide the
<a href="http://lists.boost.org/Archives/boost/2011/02/176712.php" target="_top">variadic
macro syntax</a> on compilers that support variadic macros.
</p>
<p>
Thanks to Pierre Morcello for sharing some code that experimented with implementing
local functions using <a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a>
binding (even if this library is not based on such a code).
</p>
<p>
Thanks to John Bytheway for checking the authors' virtual functor technique
that originally allowed this library to pass local functions as template parameters.
</p>
<p>
Thanks to Jeffrey Lee Hellrung for suggesting the use of the "keyword"
<code class="computeroutput"><span class="identifier">bind</span></code> to bind variables in scope
and for suggesting to use <code class="literal">bind(<span class="emphasis"><em>type</em></span>)</code>
to optionally specify the bound variable type. Thanks to Vicente Botet for
suggesting to provide a macro to expose the bound variable type to the public
API.
</p>
<p>
Thanks to Steven Watanabe, Vicente Botet, Michael Caisse, Yechezkel Mett, Joel
de Guzman, Thomas Heller, et al. for helping with the <a class="link" href="alternatives.html" title="Annex: Alternatives">Alternatives</a>
section and with the profiling of the different alternatives.
</p>
<p>
Many thanks to Jeffrey Lee Hellrung for managing the <a href="http://lists.boost.org/boost-announce/2011/12/0340.php" target="_top">Boost
review</a> of this library. Thanks also to all the people that submitted
a Boost review: Andrzej Krzemienski, Edward Diener, Gregory Crosswhite, John
Bytheway, Hartmut Kaiser, Krzysztof Czainski, Nat Lindon, Pierre Morcello,
Thomas Heller, and Vicente J. Botet. Thanks to everyone that commented on the
library during its Boost review.
</p>
<p>
Finally, many thanks to the entire <a href="http://www.boost.org" target="_top">Boost</a>
community and <a href="http://lists.boost.org" target="_top">mailing list</a> for
providing valuable comments about this library and great insights on the C++
programming language.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Lorenzo
Caminiti<p>
Distributed under the Boost Software License, Version 1.0 (see accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="bibliography.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a>
</div>
</body>
</html>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,74 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Bibliography</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="prev" href="release_notes.html" title="Release Notes">
<link rel="next" href="acknowledgments.html" title="Acknowledgments">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="release_notes.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgments.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_localfunction.bibliography"></a><a class="link" href="bibliography.html" title="Bibliography">Bibliography</a>
</h2></div></div></div>
<p>
This section lists all the bibliographic references cited by this documentation.
</p>
<p>
<a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1613.pdf" target="_top">[N1613]</a>
Thorsten Ottosen. <span class="emphasis"><em>Proposal to add Design by Contract to C++.</em></span>
The C++ Standards Committee, document no. N1613=04-0053, 2004.
</p>
<p>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2511.html" target="_top">[N2511]</a>
Alisdair Meredith. <span class="emphasis"><em>Named Lambdas and Local Functions.</em></span>
The C++ Standards Committee, document no. N2511=08-0021, 2008.
</p>
<p>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2529.pdf" target="_top">[N2529]</a>
Jaakko Jarvi, John Freeman, Lawrence Crowl. <span class="emphasis"><em>Lambda Expressions and
Closures: Wording for Monomorphic Lambdas (Revision 3).</em></span> The C++
Standards Committee, document no. N2529=08-0039, 2008.
</p>
<p>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2550.pdf" target="_top">[N2550]</a>
Jaakko Jarvi, John Freeman, Lawrence Crowl. <span class="emphasis"><em>Lambda Expressions and
Closures: Wording for Monomorphic Lambdas (Revision 4).</em></span> The C++
Standards Committee, document no. N2550=08-0060, 2008.
</p>
<p>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm" target="_top">[N2657]</a>
John Spicer. <span class="emphasis"><em>Local and Unamed Types as Template Arguments.</em></span>
The C++ Standard Committee, document no. N2657=08-0167, 2008.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Lorenzo
Caminiti<p>
Distributed under the Boost Software License, Version 1.0 (see accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="release_notes.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgments.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,710 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Examples</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="prev" href="advanced_topics.html" title="Advanced Topics">
<link rel="next" href="alternatives.html" title="Annex: Alternatives">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="advanced_topics.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="alternatives.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_localfunction.examples"></a><a class="link" href="examples.html" title="Examples">Examples</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="examples.html#boost_localfunction.examples.gcc_lambdas__without_c__11_">GCC
Lambdas (without C++11)</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_localfunction.examples.constant_blocks">Constant
Blocks</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_localfunction.examples.scope_exits">Scope Exits</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_localfunction.examples.boost_phoenix_functions">Boost.Phoenix
Functions</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_localfunction.examples.closures">Closures</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_localfunction.examples.gcc_nested_functions">GCC
Nested Functions</a></span></dt>
<dt><span class="section"><a href="examples.html#boost_localfunction.examples.n_papers">N-Papers</a></span></dt>
</dl></div>
<p>
This section lists some examples that use this library.
</p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.examples.gcc_lambdas__without_c__11_"></a><a class="link" href="examples.html#boost_localfunction.examples.gcc_lambdas__without_c__11_" title="GCC Lambdas (without C++11)">GCC
Lambdas (without C++11)</a>
</h3></div></div></div>
<p>
Combing local functions with the non-standard <a href="http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html" target="_top">statement
expression</a> extension of the GCC compiler, it is possible to implement
lambda functions for GCC compilers even without <a href="http://www.open-std.org/JTC1/SC22/WG21/" target="_top">C++11</a>
support.
</p>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
This code only works on compilers that support GCC statement expression
extension or that support <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a>.
</p></td></tr>
</table></div>
<p>
For example (see also <a href="../../../example/gcc_lambda.cpp" target="_top"><code class="literal">gcc_lambda.cpp</code></a>
and <a href="../../../example/gcc_cxx11_lambda.cpp" target="_top"><code class="literal">gcc_cxx11_lambda.cpp</code></a>):
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
With Local Functions (GCC only)
</p>
</th>
<th>
<p>
C++11 Lambdas
</p>
</th>
</tr></thead>
<tbody><tr>
<td>
<p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="keyword">int</span> <span class="identifier">val</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">nums</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">};</span>
<span class="keyword">int</span><span class="special">*</span> <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">nums</span> <span class="special">+</span> <span class="number">3</span><span class="special">;</span>
<span class="keyword">int</span><span class="special">*</span> <span class="identifier">iter</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">find_if</span><span class="special">(</span><span class="identifier">nums</span><span class="special">,</span> <span class="identifier">end</span><span class="special">,</span>
<span class="identifier">GCC_LAMBDA</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">val</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">num</span><span class="special">,</span> <span class="keyword">return</span> <span class="keyword">bool</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">return</span> <span class="identifier">num</span> <span class="special">==</span> <span class="identifier">val</span><span class="special">;</span>
<span class="special">}</span> <span class="identifier">GCC_LAMBDA_END</span>
<span class="special">);</span>
</pre>
<p>
</p>
</td>
<td>
<p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="keyword">int</span> <span class="identifier">val</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">nums</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">};</span>
<span class="keyword">int</span><span class="special">*</span> <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">nums</span> <span class="special">+</span> <span class="number">3</span><span class="special">;</span>
<span class="keyword">int</span><span class="special">*</span> <span class="identifier">iter</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">find_if</span><span class="special">(</span><span class="identifier">nums</span><span class="special">,</span> <span class="identifier">end</span><span class="special">,</span>
<span class="special">[</span><span class="identifier">val</span><span class="special">](</span><span class="keyword">int</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">-&gt;</span> <span class="keyword">bool</span> <span class="special">{</span>
<span class="keyword">return</span> <span class="identifier">num</span> <span class="special">==</span> <span class="identifier">val</span><span class="special">;</span>
<span class="special">}</span>
<span class="special">);</span>
</pre>
<p>
</p>
</td>
</tr></tbody>
</table></div>
<p>
The GCC lambda function macros are implemented using local functions (see
also <a href="../../../example/gcc_lambda.hpp" target="_top"><code class="literal">gcc_lambda.hpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor"># define</span> <span class="identifier">GCC_LAMBDA_</span><span class="special">(</span><span class="identifier">binds</span><span class="special">,</span> <span class="identifier">params</span><span class="special">,</span> <span class="identifier">results</span><span class="special">)</span> <span class="special">\</span>
<span class="special">({</span> <span class="comment">/* open statement expression (GCC extension only) */</span> <span class="special">\</span>
<span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span> <span class="special">\</span>
<span class="identifier">BOOST_PP_LIST_ENUM</span><span class="special">(</span><span class="identifier">BOOST_PP_LIST_APPEND</span><span class="special">(</span><span class="identifier">binds</span><span class="special">,</span> <span class="special">\</span>
<span class="identifier">BOOST_PP_LIST_APPEND</span><span class="special">(</span><span class="identifier">params</span><span class="special">,</span> <span class="special">\</span>
<span class="identifier">BOOST_PP_IIF</span><span class="special">(</span><span class="identifier">BOOST_PP_LIST_IS_NIL</span><span class="special">(</span><span class="identifier">results</span><span class="special">),</span> <span class="special">\</span>
<span class="special">(</span><span class="keyword">return</span> <span class="keyword">void</span><span class="special">,</span> <span class="identifier">BOOST_PP_NIL</span><span class="special">)</span> <span class="comment">/* default for lambdas */</span> <span class="special">\</span>
<span class="special">,</span> <span class="special">\</span>
<span class="identifier">results</span> <span class="special">\</span>
<span class="special">)\</span>
<span class="special">)</span> <span class="special">\</span>
<span class="special">))</span> <span class="special">\</span>
<span class="special">)</span>
</pre>
<p>
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">GCC_LAMBDA_END_</span><span class="special">(</span><span class="identifier">id</span><span class="special">)</span> <span class="special">\</span>
<span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">BOOST_PP_CAT</span><span class="special">(</span><span class="identifier">gcc_lambda_</span><span class="special">,</span> <span class="identifier">id</span><span class="special">))</span> <span class="special">\</span>
<span class="identifier">BOOST_PP_CAT</span><span class="special">(</span><span class="identifier">gcc_lambda_</span><span class="special">,</span> <span class="identifier">id</span><span class="special">);</span> <span class="special">\</span>
<span class="special">})</span> <span class="comment">/* close statement expression (GCC extension only) */</span>
</pre>
<p>
</p>
<p>
This is possible because GCC statement expressions allow to use declaration
statements within expressions and therefore to declare a local function within
an expression. The macros automatically detect if the compiler supports
<a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> in which case the implementation uses native lambdas
instead of local functions in GCC statement expressions. However, <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> do not support constant binding so it is best to
only use <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span>
<span class="identifier">variable</span></code> (same as <code class="computeroutput"><span class="special">=</span><span class="identifier">variable</span></code>
for <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a>) and <code class="computeroutput"><span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">variable</span></code>
(same as <code class="computeroutput"><span class="special">&amp;</span><span class="identifier">variable</span></code>
for <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a>') because these have the exact same semantic between
the local function and the native lambda implementations. Furthermore, local
functions allow to bind data members directly while <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> require to access data members via binding the object
<code class="computeroutput"><span class="keyword">this</span></code>. Unfortunately, the short-hand
binds <code class="computeroutput"><span class="special">&amp;</span></code> and <code class="computeroutput"><span class="special">=</span></code> of <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> (which automatically bind all variables in scope
either by reference or value) are not supported by these GCC lambda function
macros because they are not supported by local functions. Finally, the result
type <code class="computeroutput"><span class="keyword">return</span> </code><code class="literal"><span class="emphasis"><em>result-type</em></span></code>
is optional and it is assumed <code class="computeroutput"><span class="keyword">void</span></code>
when it is not specified (same as with <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a>).
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.examples.constant_blocks"></a><a class="link" href="examples.html#boost_localfunction.examples.constant_blocks" title="Constant Blocks">Constant
Blocks</a>
</h3></div></div></div>
<p>
It is possible to use local functions to check assertions between variables
that are made constant within the asserted expressions. This is advantageous
because assertions are not supposed to change the state of the program and
ideally the compiler will not compile assertions that modify variables.
</p>
<p>
For example, consider the following assertion where by mistake we programmed
<code class="computeroutput"><span class="keyword">operator</span><span class="special">=</span></code>
instead of <code class="computeroutput"><span class="keyword">operator</span><span class="special">==</span></code>:
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span> <span class="special">=</span> <span class="identifier">y</span><span class="special">);</span> <span class="comment">// Mistakenly `=` instead of `==`.</span>
</pre>
<p>
Ideally this code will not compile instead this example not only compiles
but the assertion even passes the run-time check and no error is generated
at all. The <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1613.pdf" target="_top">[N1613]</a>
paper introduces the concept of a <span class="emphasis"><em>const-block</em></span> which
could be used to wrap the assertion above and catch the programming error
at compile-time. Similarly, the following code will generate a compile-time
error when <code class="computeroutput"><span class="keyword">operator</span><span class="special">=</span></code>
is mistakenly used instead of <code class="computeroutput"><span class="keyword">operator</span><span class="special">==</span></code> because both <code class="computeroutput"><span class="identifier">x</span></code>
and <code class="computeroutput"><span class="identifier">y</span></code> are made constants
(using local functions) within the block of code performing the assertion
(see also <a href="../../../example/const_block_error.cpp" target="_top"><code class="literal">const_block_error.cpp</code></a>):
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
With Local Functions
</p>
</th>
<th>
<p>
N1613 Const-Blocks
</p>
</th>
</tr></thead>
<tbody><tr>
<td>
<p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="identifier">CONST_BLOCK</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Constant block.</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span> <span class="special">=</span> <span class="identifier">y</span><span class="special">);</span> <span class="comment">// Compiler error.</span>
<span class="special">}</span> <span class="identifier">CONST_BLOCK_END</span>
</pre>
<p>
</p>
</td>
<td>
<p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="keyword">const</span> <span class="special">{</span> <span class="comment">// Constant block.</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span> <span class="special">=</span> <span class="identifier">y</span><span class="special">);</span> <span class="comment">// Compiler error.</span>
<span class="special">}</span>
</pre>
<p>
</p>
</td>
</tr></tbody>
</table></div>
<p>
The constant block macros are implemented using local functions (see also
<a href="../../../example/const_block.hpp" target="_top"><code class="literal">const_block.hpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">CONST_BLOCK_</span><span class="special">(</span><span class="identifier">variables</span><span class="special">)</span> <span class="special">\</span>
<span class="keyword">void</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span> <span class="special">\</span>
<span class="identifier">BOOST_PP_IIF</span><span class="special">(</span><span class="identifier">BOOST_PP_LIST_IS_NIL</span><span class="special">(</span><span class="identifier">variables</span><span class="special">),</span> <span class="special">\</span>
<span class="keyword">void</span> <span class="identifier">BOOST_PP_TUPLE_EAT</span><span class="special">(</span><span class="number">3</span><span class="special">)</span> <span class="special">\</span>
<span class="special">,</span> <span class="special">\</span>
<span class="identifier">BOOST_PP_LIST_FOR_EACH_I</span> <span class="special">\</span>
<span class="special">)(</span><span class="identifier">CONST_BLOCK_BIND_</span><span class="special">,</span> <span class="special">~,</span> <span class="identifier">variables</span><span class="special">)</span> <span class="special">\</span>
<span class="special">)</span>
</pre>
<p>
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">CONST_BLOCK_END_</span><span class="special">(</span><span class="identifier">id</span><span class="special">)</span> <span class="special">\</span>
<span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">BOOST_PP_CAT</span><span class="special">(</span><span class="identifier">const_block_</span><span class="special">,</span> <span class="identifier">id</span><span class="special">))</span> <span class="special">\</span>
<span class="identifier">BOOST_PP_CAT</span><span class="special">(</span><span class="identifier">const_block_</span><span class="special">,</span> <span class="identifier">id</span><span class="special">)();</span> <span class="comment">/* call local function immediately */</span>
</pre>
<p>
</p>
<p>
The constant block macros are implemented using a local function which binds
by constant reference <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&amp;</span></code>
all the specified variables (so the variables are constant within the code
block but they do not need to be <code class="computeroutput"><span class="identifier">CopyConstructible</span></code>
and no extra copy is performed). The local function executes the <code class="computeroutput"><span class="identifier">assert</span></code> instruction in its body and it is
called immediately after it is defined. More in general, constant blocks
can be used to evaluate any instruction (not just assertions) within a block
were all specified variables are constant.
</p>
<p>
Unfortunately, constant blocks cannot be implemented with <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> because these do not support constant binding. Variables
bound by value using <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> (<code class="computeroutput"><span class="identifier">variable</span></code>,
<code class="computeroutput"><span class="special">=</span><span class="identifier">variable</span></code>,
and <code class="computeroutput"><span class="special">=</span></code>) are constant but they
are required to be <code class="computeroutput"><span class="identifier">CopyConstructible</span></code>
and they introduce potentially expensive copy operations. <a href="#ftn.boost_localfunction.examples.constant_blocks.f0" class="footnote" name="boost_localfunction.examples.constant_blocks.f0"><sup class="footnote">[29]</sup></a> Of course it is always possible to introduce extra constant variables
and bind these variables to the <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> but the constant block code will then have to manage
the declaration and initialization of these extra variables plus it will
have to use the extra variable names instead of the original variable names:
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="keyword">const</span> <span class="keyword">decltype</span><span class="special">(</span><span class="identifier">x</span><span class="special">)&amp;</span> <span class="identifier">const_x</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">;</span> <span class="comment">// Constant so cannot be modified</span>
<span class="keyword">const</span> <span class="keyword">decltype</span><span class="special">(</span><span class="identifier">y</span><span class="special">)&amp;</span> <span class="identifier">const_y</span> <span class="special">=</span> <span class="identifier">y</span><span class="special">;</span> <span class="comment">// and reference so no copy.</span>
<span class="special">[&amp;</span><span class="identifier">const_x</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">const_y</span><span class="special">]()</span> <span class="special">{</span> <span class="comment">// Lambda functions (C++11 only).</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">const_x</span> <span class="special">=</span> <span class="identifier">const_y</span><span class="special">);</span> <span class="comment">// Unfortunately, `const_` names.</span>
<span class="special">}();</span>
</pre>
<p>
</p>
<p>
In many cases the use of an extra constant variable <code class="computeroutput"><span class="identifier">const_x</span></code>
can be acceptable but in other cases it might be preferable to maintain the
same variable name <code class="computeroutput"><span class="identifier">x</span></code> within
the function body.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.examples.scope_exits"></a><a class="link" href="examples.html#boost_localfunction.examples.scope_exits" title="Scope Exits">Scope Exits</a>
</h3></div></div></div>
<p>
Scope exits allow to execute arbitrary code at the exit of the enclosing
scope and they are provided by the <a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a>
library.
</p>
<p>
For curiosity, here we show how to re-implement scope exits using local functions.
One small advantage of scope exits that use local functions is that they
support constant binding. <a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a>
does not directly support constant binding (however, it is always possible
to introduce an extra <code class="computeroutput"><span class="keyword">const</span></code>
local variable, assign it to the value to bind, and then bind the <code class="computeroutput"><span class="keyword">const</span></code> variable so to effectively have constant
binding with <a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a>
as well).
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
In general, the authors recommend to use <a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a>
instead of the code listed by this example whenever possible (because
<a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a>
is a library deliberately designed to support the scope exit construct).
</p></td></tr>
</table></div>
<p>
The following example binds <code class="computeroutput"><span class="identifier">p</span></code>
by constant reference so this variable cannot be modified within the scope
exit body but it is not copied and it will present the value it has at the
exit of the enclosing scope and not at the scope exit declaration (see also
<a href="../../../example/scope_exit.cpp" target="_top"><code class="literal">scope_exit.cpp</code></a>):
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
With Local Functions
</p>
</th>
<th>
<p>
Boost.ScopeExit
</p>
</th>
</tr></thead>
<tbody><tr>
<td>
<p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="identifier">person</span><span class="special">&amp;</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">persons_</span><span class="special">.</span><span class="identifier">back</span><span class="special">();</span>
<span class="identifier">person</span><span class="special">::</span><span class="identifier">evolution_t</span> <span class="identifier">checkpoint</span> <span class="special">=</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">evolution_</span><span class="special">;</span>
<span class="identifier">SCOPE_EXIT</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">checkpoint</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">bind</span> <span class="identifier">this_</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">checkpoint</span> <span class="special">==</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">evolution_</span><span class="special">)</span> <span class="identifier">this_</span><span class="special">-&gt;</span><span class="identifier">persons_</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span>
<span class="special">}</span> <span class="identifier">SCOPE_EXIT_END</span>
</pre>
<p>
</p>
</td>
<td>
<p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="identifier">person</span><span class="special">&amp;</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">persons_</span><span class="special">.</span><span class="identifier">back</span><span class="special">();</span>
<span class="identifier">person</span><span class="special">::</span><span class="identifier">evolution_t</span> <span class="identifier">checkpoint</span> <span class="special">=</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">evolution_</span><span class="special">;</span>
<span class="identifier">BOOST_SCOPE_EXIT</span><span class="special">(</span><span class="identifier">checkpoint</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">p</span><span class="special">,</span> <span class="identifier">this_</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Or extra variable `const_p`.</span>
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">checkpoint</span> <span class="special">==</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">evolution_</span><span class="special">)</span> <span class="identifier">this_</span><span class="special">-&gt;</span><span class="identifier">persons_</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span>
<span class="special">}</span> <span class="identifier">BOOST_SCOPE_EXIT_END</span>
</pre>
<p>
</p>
</td>
</tr></tbody>
</table></div>
<p>
The scope exit macros are implemented by passing a local function when constructing
an object of the following class (see also <a href="../../../example/scope_exit.hpp" target="_top"><code class="literal">scope_exit.hpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">scope_exit</span> <span class="special">{</span>
<span class="identifier">scope_exit</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">void</span> <span class="special">(</span><span class="keyword">void</span><span class="special">)&gt;</span> <span class="identifier">f</span><span class="special">):</span> <span class="identifier">f_</span><span class="special">(</span><span class="identifier">f</span><span class="special">)</span> <span class="special">{}</span>
<span class="special">~</span><span class="identifier">scope_exit</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">f_</span><span class="special">();</span> <span class="special">}</span>
<span class="keyword">private</span><span class="special">:</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">void</span> <span class="special">(</span><span class="keyword">void</span><span class="special">)&gt;</span> <span class="identifier">f_</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor"># define</span> <span class="identifier">SCOPE_EXIT</span><span class="special">(...)</span> <span class="special">\</span>
<span class="keyword">void</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="identifier">__VA_ARGS__</span><span class="special">)</span>
</pre>
<p>
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">SCOPE_EXIT_END_</span><span class="special">(</span><span class="identifier">id</span><span class="special">)</span> <span class="special">\</span>
<span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">BOOST_PP_CAT</span><span class="special">(</span><span class="identifier">scope_exit_func_</span><span class="special">,</span> <span class="identifier">id</span><span class="special">))</span> <span class="special">\</span>
<span class="identifier">scope_exit</span> <span class="identifier">BOOST_PP_CAT</span><span class="special">(</span><span class="identifier">scope_exit_</span><span class="special">,</span> <span class="identifier">id</span><span class="special">)(</span> <span class="special">\</span>
<span class="identifier">BOOST_PP_CAT</span><span class="special">(</span><span class="identifier">scope_exit_func_</span><span class="special">,</span> <span class="identifier">id</span><span class="special">));</span>
</pre>
<p>
</p>
<p>
A local variable within the enclosing scope is used to hold the object so
the destructor will be invoked at the exit of the enclosing scope and it
will in turn call the local function executing the scope exit instructions.
The scope exit local function has no parameter and <code class="computeroutput"><span class="keyword">void</span></code>
result type but it supports binding and constant binding.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.examples.boost_phoenix_functions"></a><a class="link" href="examples.html#boost_localfunction.examples.boost_phoenix_functions" title="Boost.Phoenix Functions">Boost.Phoenix
Functions</a>
</h3></div></div></div>
<p>
Local functions can be used to create <a href="http://www.boost.org/libs/phoenix" target="_top">Boost.Phoenix</a>
functions. For example (see also <a href="../../../example/phoenix_factorial_local.cpp" target="_top"><code class="literal">phoenix_factorial_local.cpp</code></a>
and <a href="../../../example/phoenix_factorial.cpp" target="_top"><code class="literal">phoenix_factorial.cpp</code></a>):
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Local Functions
</p>
</th>
<th>
<p>
Global Functor
</p>
</th>
</tr></thead>
<tbody><tr>
<td>
<p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">arg_names</span><span class="special">::</span><span class="identifier">arg1</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Unfortunately, monomorphic.</span>
<span class="keyword">return</span> <span class="special">(</span><span class="identifier">n</span> <span class="special">&lt;=</span> <span class="number">0</span><span class="special">)</span> <span class="special">?</span> <span class="number">1</span> <span class="special">:</span> <span class="identifier">n</span> <span class="special">*</span> <span class="identifier">factorial_impl</span><span class="special">(</span><span class="identifier">n</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">recursive</span> <span class="identifier">factorial_impl</span><span class="special">)</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">int</span> <span class="special">(</span><span class="keyword">int</span><span class="special">)&gt;</span> <span class="special">&gt;</span>
<span class="identifier">factorial</span><span class="special">(</span><span class="identifier">factorial_impl</span><span class="special">);</span> <span class="comment">// Phoenix function from local function.</span>
<span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">4</span><span class="special">;</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">factorial</span><span class="special">(</span><span class="identifier">i</span><span class="special">)()</span> <span class="special">==</span> <span class="number">24</span><span class="special">);</span> <span class="comment">// Call.</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">factorial</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">)(</span><span class="identifier">i</span><span class="special">)</span> <span class="special">==</span> <span class="number">24</span><span class="special">);</span> <span class="comment">// Lazy call.</span>
<span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
</p>
</td>
<td>
<p>
</p>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="keyword">struct</span> <span class="identifier">factorial_impl</span> <span class="special">{</span> <span class="comment">// Phoenix function from global functor.</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Sig</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">result</span><span class="special">;</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">This</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Arg</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">result</span><span class="special">&lt;</span><span class="identifier">This</span> <span class="special">(</span><span class="identifier">Arg</span><span class="special">)&gt;</span> <span class="special">:</span> <span class="identifier">result</span><span class="special">&lt;</span><span class="identifier">This</span> <span class="special">(</span><span class="identifier">Arg</span> <span class="keyword">const</span><span class="special">&amp;)&gt;</span> <span class="special">{};</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">This</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Arg</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">result</span><span class="special">&lt;</span><span class="identifier">This</span> <span class="special">(</span><span class="identifier">Arg</span><span class="special">&amp;)&gt;</span> <span class="special">{</span> <span class="keyword">typedef</span> <span class="identifier">Arg</span> <span class="identifier">type</span><span class="special">;</span> <span class="special">};</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Arg</span><span class="special">&gt;</span> <span class="comment">// Polymorphic.</span>
<span class="identifier">Arg</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Arg</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
<span class="keyword">return</span> <span class="special">(</span><span class="identifier">n</span> <span class="special">&lt;=</span> <span class="number">0</span><span class="special">)</span> <span class="special">?</span> <span class="number">1</span> <span class="special">:</span> <span class="identifier">n</span> <span class="special">*</span> <span class="special">(*</span><span class="keyword">this</span><span class="special">)(</span><span class="identifier">n</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span>
<span class="special">}</span>
<span class="special">};</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">arg_names</span><span class="special">::</span><span class="identifier">arg1</span><span class="special">;</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">phoenix</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="identifier">factorial_impl</span><span class="special">&gt;</span> <span class="identifier">factorial</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">4</span><span class="special">;</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">factorial</span><span class="special">(</span><span class="identifier">i</span><span class="special">)()</span> <span class="special">==</span> <span class="number">24</span><span class="special">);</span> <span class="comment">// Call.</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">factorial</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">)(</span><span class="identifier">i</span><span class="special">)</span> <span class="special">==</span> <span class="number">24</span><span class="special">);</span> <span class="comment">// Lazy call.</span>
<span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
</p>
</td>
</tr></tbody>
</table></div>
<p>
This is presented here mainly as a curiosity because <a href="http://www.boost.org/libs/phoenix" target="_top">Boost.Phoenix</a>
functions created from local functions have the important limitation that
they cannot be polymorphic. <a href="#ftn.boost_localfunction.examples.boost_phoenix_functions.f0" class="footnote" name="boost_localfunction.examples.boost_phoenix_functions.f0"><sup class="footnote">[30]</sup></a> Therefore, in many cases creating the <a href="http://www.boost.org/libs/phoenix" target="_top">Boost.Phoenix</a>
function from global functors (possibly with the help of <a href="http://www.boost.org/libs/phoenix" target="_top">Boost.Phoenix</a>
adaptor macros) might be a more useful.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.examples.closures"></a><a class="link" href="examples.html#boost_localfunction.examples.closures" title="Closures">Closures</a>
</h3></div></div></div>
<p>
The following are examples of <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)" target="_top">closures</a>
that illustrate how to return local functions to the calling scope (note
how extra care is taken in order to ensure that all bound variables remain
valid at the calling scope):
</p>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
<p>
Files
</p>
</th></tr></thead>
<tbody>
<tr><td>
<p>
<a href="../../../test/return_inc.cpp" target="_top"><code class="literal">return_inc.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/return_this.cpp" target="_top"><code class="literal">return_this.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/return_setget.cpp" target="_top"><code class="literal">return_setget.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/return_derivative.cpp" target="_top"><code class="literal">return_derivative.cpp</code></a>
</p>
</td></tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.examples.gcc_nested_functions"></a><a class="link" href="examples.html#boost_localfunction.examples.gcc_nested_functions" title="GCC Nested Functions">GCC
Nested Functions</a>
</h3></div></div></div>
<p>
The GCC C compiler supports local functions as a non-standard extension under
the name of <a href="http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html" target="_top">nested
functions</a>. Note that nested functions are exclusively a C extension
of the GCC compiler (they are not supported for C++ not even by the GCC compiler,
and they are not part of any C or C++ standard, nor they are supported by
other compilers like MSVC).
</p>
<p>
The following examples are taken form the GCC nested function documentation
and programmed using local functions:
</p>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
<p>
Files
</p>
</th></tr></thead>
<tbody>
<tr><td>
<p>
<a href="../../../example/gcc_square.cpp" target="_top"><code class="literal">gcc_square.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../example/gcc_access.cpp" target="_top"><code class="literal">gcc_access.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../example/gcc_store.cpp" target="_top"><code class="literal">gcc_store.cpp</code></a>
</p>
</td></tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.examples.n_papers"></a><a class="link" href="examples.html#boost_localfunction.examples.n_papers" title="N-Papers">N-Papers</a>
</h3></div></div></div>
<p>
The following examples are taken from different C++ "N-papers"
and programmed using local functions:
</p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
Files
</p>
</th>
<th>
<p>
Notes
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<a href="../../../example/n2550_find_if.cpp" target="_top"><code class="literal">n2550_find_if.cpp</code></a>
</p>
</td>
<td>
<p>
This example is adapted from <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2550.pdf" target="_top">[N2550]</a>
(<a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a>): It passes a local function to the STL
algorithm <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">find_if</span></code>.
</p>
</td>
</tr>
<tr>
<td>
<p>
<a href="../../../example/n2529_this.cpp" target="_top"><code class="literal">n2529_this.cpp</code></a>
</p>
</td>
<td>
<p>
This example is adapted from <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2529.pdf" target="_top">[N2529]</a>
(<a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a>): It binds the object in scope <code class="computeroutput"><span class="keyword">this</span></code> to a local function.
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
<div id="ftn.boost_localfunction.examples.constant_blocks.f0" class="footnote"><p><a href="#boost_localfunction.examples.constant_blocks.f0" class="para"><sup class="para">[29] </sup></a>
Ideally, <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> would allow to bind variables also using <code class="computeroutput"><span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">variable</span></code> (constant reference) and <code class="computeroutput"><span class="keyword">const</span><span class="special">&amp;</span></code>
(all variables by constant reference).
</p></div>
<div id="ftn.boost_localfunction.examples.boost_phoenix_functions.f0" class="footnote"><p><a href="#boost_localfunction.examples.boost_phoenix_functions.f0" class="para"><sup class="para">[30] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> Local functions can only be
monomorphic because they are implemented using local classes and local
classes cannot be templates in C++ (not even in <a href="http://www.open-std.org/JTC1/SC22/WG21/" target="_top">C++11</a>).
</p></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Lorenzo
Caminiti<p>
Distributed under the Boost Software License, Version 1.0 (see accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="advanced_topics.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="alternatives.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,192 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Getting Started</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="prev" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="next" href="tutorial.html" title="Tutorial">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tutorial.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_localfunction.getting_started"></a><a class="link" href="getting_started.html" title="Getting Started">Getting Started</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="getting_started.html#boost_localfunction.getting_started.this_documentation">This
Documentation</a></span></dt>
<dt><span class="section"><a href="getting_started.html#boost_localfunction.getting_started.compilers_and_platforms">Compilers
and Platforms</a></span></dt>
<dt><span class="section"><a href="getting_started.html#boost_localfunction.getting_started.installation">Installation</a></span></dt>
</dl></div>
<p>
This section explains how to setup a system to use this library.
</p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.getting_started.this_documentation"></a><a class="link" href="getting_started.html#boost_localfunction.getting_started.this_documentation" title="This Documentation">This
Documentation</a>
</h3></div></div></div>
<p>
Programmers should have enough knowledge to use this library after reading
the <a class="link" href="../index.html#boost_localfunction.introduction" title="Introduction">Introduction</a>,
<a class="link" href="getting_started.html" title="Getting Started">Getting Started</a>,
and <a class="link" href="tutorial.html" title="Tutorial">Tutorial</a> sections.
The <a class="link" href="advanced_topics.html" title="Advanced Topics">Advanced Topics</a>
and <a href="../reference.html" target="_top">Reference</a> sections can be consulted
at a later point to gain a more advanced knowledge of the library. All the
other sections of this documentation can be considered optional.
</p>
<p>
Some footnotes are marked by the word "<span class="bold"><strong>Rationale</strong></span>".
They explain reasons behind decisions made during the design and implementation
of this library.
</p>
<p>
In most of the examples presented in this documentation, the Boost.Detail/LightweightTest
(<code class="literal">boost/detail/lightweight_test.hpp</code>) macro <code class="computeroutput"><span class="identifier">BOOST_TEST</span></code> is used to check correctness
conditions. The <code class="computeroutput"><span class="identifier">BOOST_TEST</span></code>
macro is conceptually similar to <code class="computeroutput"><span class="identifier">assert</span></code>
but a failure of the checked condition does not abort the program, instead
it makes <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span></code> return a non-zero program
exit code. <a href="#ftn.boost_localfunction.getting_started.this_documentation.f0" class="footnote" name="boost_localfunction.getting_started.this_documentation.f0"><sup class="footnote">[3]</sup></a>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.getting_started.compilers_and_platforms"></a><a class="link" href="getting_started.html#boost_localfunction.getting_started.compilers_and_platforms" title="Compilers and Platforms">Compilers
and Platforms</a>
</h3></div></div></div>
<p>
The implementation of this library uses preprocessor and template meta-programming
(as supported by <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a>
and <a href="http://www.boost.org/libs/mpl" target="_top">Boost.MPL</a>), templates
with partial specializations and function pointers (similarly to <a href="http://www.boost.org/libs/function" target="_top">Boost.Function</a>),
and automatic type deduction (as supported by <a href="http://www.boost.org/libs/typeof" target="_top">Boost.Typeof</a>).
The authors originally developed and tested the library on:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
GNU Compiler Collection (GCC) C++ 4.5.1 on Ubuntu Linux 10.
</li>
<li class="listitem">
GCC 4.3.4 and 4.5.3 (with and without <a href="http://www.open-std.org/JTC1/SC22/WG21/" target="_top">C++11</a>
features enabled <code class="computeroutput"><span class="special">-</span><span class="identifier">std</span><span class="special">=</span><span class="identifier">c</span><span class="special">++</span><span class="number">0</span><span class="identifier">x</span></code>)
on Cygwin.
</li>
<li class="listitem">
Miscrosoft Visual C++ (MSVC) 8.0 on Windows XP and Windows 7.
</li>
</ol></div>
<p>
See the library <a href="http://www.boost.org/development/tests/release/developer/local_function.html" target="_top">regressions
test results</a> for detailed information on supported compilers and
platforms.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.getting_started.installation"></a><a class="link" href="getting_started.html#boost_localfunction.getting_started.installation" title="Installation">Installation</a>
</h3></div></div></div>
<p>
This library is composed of header files only. Therefore there is no pre-compiled
object file which needs to be installed or linked. Programmers can simply
instruct the C++ compiler where to find the library header files (<code class="computeroutput"><span class="special">-</span><span class="identifier">I</span></code> option
for GCC, <code class="computeroutput"><span class="special">/</span><span class="identifier">I</span></code>
option for MSVC, etc) and they can start compiling code using this library.
</p>
<p>
The library implementation uses <a href="http://www.boost.org/libs/typeof" target="_top">Boost.Typeof</a>
to automatically deduce the types of bound variables (see the <a class="link" href="tutorial.html" title="Tutorial">Tutorial</a>
section). In order to compile code in type-of emulation mode, all types should
be properly registered using <code class="computeroutput"><span class="identifier">BOOST_TYPEOF_REGISTER_TYPE</span></code>
and <code class="computeroutput"><span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span></code>,
or appropriate <a href="http://www.boost.org/libs/typeof" target="_top">Boost.Typeof</a>
headers should be included (see the source code of most examples presented
in this documentation).
</p>
<p>
The followings are part of the library private API, they are not documented,
and they should not be directly used by programmers: <a href="#ftn.boost_localfunction.getting_started.installation.f0" class="footnote" name="boost_localfunction.getting_started.installation.f0"><sup class="footnote">[4]</sup></a>
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Any symbol defined by files within the <code class="literal">boost/local_function/aux_/</code>
or <code class="literal">boost/local_function/detail/</code> directory (these header
files should not be directly included by programmers).
</li>
<li class="listitem">
Any symbol within the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">local_function</span><span class="special">::</span><span class="identifier">aux</span></code>
or <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">local_function</span><span class="special">::</span><span class="identifier">detail</span></code> namespace.
</li>
<li class="listitem">
Any symbol prefixed by <code class="computeroutput"><span class="identifier">boost_local_function_aux_</span><span class="special">...</span></code> or <code class="computeroutput"><span class="identifier">boost_local_function_detail_</span><span class="special">...</span></code> (regardless of its namespace).
</li>
<li class="listitem">
Any symbol prefixed by <code class="computeroutput"><span class="identifier">BOOST_LOCAL_FUNCTION_AUX_</span><span class="special">...</span></code> or <code class="computeroutput"><span class="identifier">BOOST_LOCAL_FUNCTION_DETAIL_</span><span class="special">...</span></code> (regardless of its namespace).
</li>
</ul></div>
<p>
Some of the library behaviour can be changed at compile-time by defining
special <span class="emphasis"><em>configuration macros</em></span>. If a configuration macro
is left undefined, the library will use an appropriate default value for
it. All configuration macros are defined in the header file <code class="computeroutput">boost/local_function/config.hpp</code>.
It is strongly recommended not to change the library configuration macro
definitions unless strictly necessary.
</p>
</div>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
<div id="ftn.boost_localfunction.getting_started.this_documentation.f0" class="footnote"><p><a href="#boost_localfunction.getting_started.this_documentation.f0" class="para"><sup class="para">[3] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> Using Boost.Detail/LightweightTest
allows to add the examples to the library regression tests so to make sure
that they always compile and run correctly.
</p></div>
<div id="ftn.boost_localfunction.getting_started.installation.f0" class="footnote"><p><a href="#boost_localfunction.getting_started.installation.f0" class="para"><sup class="para">[4] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> This library concatenates symbols
specified by the programmers (e.g., the local function name) with other
symbols (e.g., special prefixes or file line numbers) to make internal
symbols with unique names to avoid name clashes. These symbols are separated
by the letter "<code class="computeroutput"><span class="identifier">X</span></code>"
when they are concatenated so they read more easily during debugging (the
underscore character "<code class="computeroutput"><span class="identifier">_</span></code>"
could not be used instead of the letter "<code class="computeroutput"><span class="identifier">X</span></code>"
because if the original symbols already contained a leading or trailing
underscore, the concatenation could result in a symbol with double underscores
"<code class="computeroutput"><span class="identifier">__</span></code>" which is
reserved by the C++ standard). The "aux" symbols are private
to this library while the "detail" symbols may be used within
Boost by other libraries but they are still not part of this library public
API.
</p></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Lorenzo
Caminiti<p>
Distributed under the Boost Software License, Version 1.0 (see accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tutorial.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,230 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Annex: Implementation</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="prev" href="no_variadic_macros.html" title="Annex: No Variadic Macros">
<link rel="next" href="../reference.html" title="Reference">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="no_variadic_macros.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../reference.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_localfunction.implementation"></a><a class="link" href="implementation.html" title="Annex: Implementation">Annex: Implementation</a>
</h2></div></div></div>
<p>
This section gives an overview of the key programming techniques used to implement
this library.
</p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
The code listed here can be used by curious readers and library maintainers
as a reference in trying to understand the library source code. There is
absolutely no guarantee that the library implementation uses the exact code
listed here.
</p></td></tr>
</table></div>
<h4>
<a name="boost_localfunction.implementation.h0"></a>
<span class="phrase"><a name="boost_localfunction.implementation.local_classes_as_template_parameters"></a></span><a class="link" href="implementation.html#boost_localfunction.implementation.local_classes_as_template_parameters">Local
Classes as Template Parameters</a>
</h4>
<p>
This library uses a local class to implement the local function object. However,
in <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/standards" target="_top">C++03</a>
local classes (and therefore the local function objects they implement) cannot
be passed as template parameters (e.g., to the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span></code>
algorithm), this is instead possible in <a href="http://www.open-std.org/JTC1/SC22/WG21/" target="_top">C++11</a>,
MSVC, and some other compilers (see <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm" target="_top">[N2657]</a>
and <a href="http://www.boost.org/libs/chrono" target="_top">Boost.Config</a>'s <code class="computeroutput"><span class="identifier">BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS</span></code>).
To work around this limitation, this library investigated the following two
"tricks" (both tricks can be extended to support function default
parameters):
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
The <span class="emphasis"><em>casting functor trick</em></span> uses a non-local functor
that calls a static member function of the local class via a function pointer.
The static member function then calls the correct local function body after
type casting the object from a <code class="computeroutput"><span class="keyword">void</span><span class="special">*</span></code> pointer (local classes can always be used
for type casting via <code class="computeroutput"><span class="keyword">static_cast</span></code>
or similar).
</li>
<li class="listitem">
The <span class="emphasis"><em>virtual functor trick</em></span> derives the local functor
class from a non-local base class. The correct overridden implementation
of the virtual <code class="computeroutput"><span class="keyword">operator</span><span class="special">()</span></code>
is then called via dynamic binding.
</li>
</ol></div>
<p>
For example (see also <a href="../../../example/impl_tparam_tricks.cpp" target="_top"><code class="literal">impl_tparam_tricks.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">detail</span><span class="special">/</span><span class="identifier">lightweight_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">algorithm</span><span class="special">&gt;</span>
<span class="comment">// Casting functor trick.</span>
<span class="keyword">struct</span> <span class="identifier">casting_func</span> <span class="special">{</span>
<span class="keyword">explicit</span> <span class="identifier">casting_func</span><span class="special">(</span><span class="keyword">void</span><span class="special">*</span> <span class="identifier">obj</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(*</span><span class="identifier">call</span><span class="special">)(</span><span class="keyword">void</span><span class="special">*,</span> <span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;))</span>
<span class="special">:</span> <span class="identifier">obj_</span><span class="special">(</span><span class="identifier">obj</span><span class="special">),</span> <span class="identifier">call_</span><span class="special">(</span><span class="identifier">call</span><span class="special">)</span> <span class="special">{}</span>
<span class="comment">// Unfortunately, function pointer call is not inlined.</span>
<span class="keyword">inline</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">call_</span><span class="special">(</span><span class="identifier">obj_</span><span class="special">,</span> <span class="identifier">num</span><span class="special">);</span> <span class="special">}</span>
<span class="keyword">private</span><span class="special">:</span>
<span class="keyword">void</span><span class="special">*</span> <span class="identifier">obj_</span><span class="special">;</span>
<span class="keyword">void</span> <span class="special">(*</span><span class="identifier">call_</span><span class="special">)(</span><span class="keyword">void</span><span class="special">*,</span> <span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;);</span>
<span class="special">};</span>
<span class="comment">// Virtual functor trick.</span>
<span class="keyword">struct</span> <span class="identifier">virtual_func</span> <span class="special">{</span>
<span class="keyword">struct</span> <span class="identifier">interface</span> <span class="special">{</span>
<span class="comment">// Unfortunately, virtual function call is not inlined.</span>
<span class="keyword">inline</span> <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;)</span> <span class="special">{}</span>
<span class="special">};</span>
<span class="keyword">explicit</span> <span class="identifier">virtual_func</span><span class="special">(</span><span class="identifier">interface</span><span class="special">&amp;</span> <span class="identifier">func</span><span class="special">):</span> <span class="identifier">func_</span><span class="special">(&amp;</span><span class="identifier">func</span><span class="special">)</span> <span class="special">{}</span>
<span class="keyword">inline</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span> <span class="special">(*</span><span class="identifier">func_</span><span class="special">)(</span><span class="identifier">num</span><span class="special">);</span> <span class="special">}</span>
<span class="keyword">private</span><span class="special">:</span>
<span class="identifier">interface</span><span class="special">*</span> <span class="identifier">func_</span><span class="special">;</span>
<span class="special">};</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">int</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">factor</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span>
<span class="comment">// Local class for local function.</span>
<span class="keyword">struct</span> <span class="identifier">local_add</span> <span class="special">:</span> <span class="identifier">virtual_func</span><span class="special">::</span><span class="identifier">interface</span> <span class="special">{</span>
<span class="keyword">explicit</span> <span class="identifier">local_add</span><span class="special">(</span><span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">_sum</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">_factor</span><span class="special">)</span>
<span class="special">:</span> <span class="identifier">sum_</span><span class="special">(</span><span class="identifier">_sum</span><span class="special">),</span> <span class="identifier">factor_</span><span class="special">(</span><span class="identifier">_factor</span><span class="special">)</span> <span class="special">{}</span>
<span class="keyword">inline</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">body</span><span class="special">(</span><span class="identifier">sum_</span><span class="special">,</span> <span class="identifier">factor_</span><span class="special">,</span> <span class="identifier">num</span><span class="special">);</span>
<span class="special">}</span>
<span class="keyword">inline</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">call</span><span class="special">(</span><span class="keyword">void</span><span class="special">*</span> <span class="identifier">obj</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">local_add</span><span class="special">*</span> <span class="identifier">self</span> <span class="special">=</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">local_add</span><span class="special">*&gt;(</span><span class="identifier">obj</span><span class="special">);</span>
<span class="identifier">self</span><span class="special">-&gt;</span><span class="identifier">body</span><span class="special">(</span><span class="identifier">self</span><span class="special">-&gt;</span><span class="identifier">sum_</span><span class="special">,</span> <span class="identifier">self</span><span class="special">-&gt;</span><span class="identifier">factor_</span><span class="special">,</span> <span class="identifier">num</span><span class="special">);</span>
<span class="special">}</span>
<span class="keyword">private</span><span class="special">:</span>
<span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">sum_</span><span class="special">;</span>
<span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">factor_</span><span class="special">;</span>
<span class="keyword">inline</span> <span class="keyword">void</span> <span class="identifier">body</span><span class="special">(</span><span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">sum</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">factor</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">sum</span> <span class="special">+=</span> <span class="identifier">factor</span> <span class="special">*</span> <span class="identifier">num</span><span class="special">;</span>
<span class="special">}</span>
<span class="special">}</span> <span class="identifier">add_local</span><span class="special">(</span><span class="identifier">sum</span><span class="special">,</span> <span class="identifier">factor</span><span class="special">);</span>
<span class="identifier">casting_func</span> <span class="identifier">add_casting</span><span class="special">(&amp;</span><span class="identifier">add_local</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">local_add</span><span class="special">::</span><span class="identifier">call</span><span class="special">);</span>
<span class="identifier">virtual_func</span> <span class="identifier">add_virtual</span><span class="special">(</span><span class="identifier">add_local</span><span class="special">);</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">(</span><span class="number">10</span><span class="special">);</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">fill</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="number">1</span><span class="special">);</span>
<span class="comment">// std::for_each(v.begin(), v.end(), add_local); // Error but OK on C++11.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">add_casting</span><span class="special">);</span> <span class="comment">// OK.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">add_virtual</span><span class="special">);</span> <span class="comment">// OK.</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">sum</span> <span class="special">==</span> <span class="number">200</span><span class="special">);</span>
<span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
The casting functor trick measured slightly better run-time performances than
the virtual functor trick so the current implementation of this library uses
the casting functor trick (probably because in addition to the indirect function
call, the virtual functor trick also requires accessing the <a href="http://en.wikipedia.org/wiki/Virtual_method_table" target="_top">virtual
function table</a>). However, neither one of the two tricks was observed
to allow for compiler optimizations that inline the local function calls (because
they rely on one indirect function call via either a function pointer or a
virtual function respectively). Therefore, on compilers that accept local classes
as template parameters (MSVC, <a href="http://www.open-std.org/JTC1/SC22/WG21/" target="_top">C++11</a>,
etc, see <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm" target="_top">[N2657]</a>
and <a href="http://www.boost.org/libs/chrono" target="_top">Boost.Config</a>'s <code class="computeroutput"><span class="identifier">BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS</span></code>),
this library automatically generates code that passes the local class type
directly as template parameter without using neither one of these two tricks
in order to take full advantage of compiler optimizations that inline the local
function calls.
</p>
<h4>
<a name="boost_localfunction.implementation.h1"></a>
<span class="phrase"><a name="boost_localfunction.implementation.parsing_macros"></a></span><a class="link" href="implementation.html#boost_localfunction.implementation.parsing_macros">Parsing
Macros</a>
</h4>
<p>
This library macros can parse the list of specified parameters and detect if
any of the bound variable names matches the token <code class="computeroutput"><span class="identifier">this_</span></code>
(to generate special code to bind the object in scope), or if the variable
is bound by <code class="computeroutput"><span class="keyword">const</span></code> (to generate
special code to bind by constant), etc. The parameter tokens are inspected
using preprocessor meta-programming and specifically using the macros defined
by the files in the <code class="literal">boost/local_function/detail/preprocessor/keyword/</code>
directory. <a href="#ftn.boost_localfunction.implementation.f0" class="footnote" name="boost_localfunction.implementation.f0"><sup class="footnote">[31]</sup></a>
</p>
<p>
For example, the following code defines a macro that allows the preprocessor
to detect if a set of space-separated tokens ends with <code class="computeroutput"><span class="identifier">this_</span></code>
or not (see also <a href="../../../example/impl_pp_keyword.cpp" target="_top"><code class="literal">impl_pp_keyword.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">local_function</span><span class="special">/</span><span class="identifier">detail</span><span class="special">/</span><span class="identifier">preprocessor</span><span class="special">/</span><span class="identifier">keyword</span><span class="special">/</span><span class="identifier">thisunderscore</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">local_function</span><span class="special">/</span><span class="identifier">detail</span><span class="special">/</span><span class="identifier">preprocessor</span><span class="special">/</span><span class="identifier">keyword</span><span class="special">/</span><span class="keyword">const</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">local_function</span><span class="special">/</span><span class="identifier">detail</span><span class="special">/</span><span class="identifier">preprocessor</span><span class="special">/</span><span class="identifier">keyword</span><span class="special">/</span><span class="identifier">bind</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">detail</span><span class="special">/</span><span class="identifier">lightweight_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="comment">// Expand to 1 if space-separated tokens end with `this_`, 0 otherwise.</span>
<span class="preprocessor">#define</span> <span class="identifier">IS_THIS_BACK</span><span class="special">(</span><span class="identifier">tokens</span><span class="special">)</span> <span class="special">\</span>
<span class="identifier">BOOST_LOCAL_FUNCTION_DETAIL_PP_KEYWORD_IS_THISUNDERSCORE_BACK</span><span class="special">(</span> <span class="special">\</span>
<span class="identifier">BOOST_LOCAL_FUNCTION_DETAIL_PP_KEYWORD_BIND_REMOVE_FRONT</span><span class="special">(</span> <span class="special">\</span>
<span class="identifier">BOOST_LOCAL_FUNCTION_DETAIL_PP_KEYWORD_CONST_REMOVE_FRONT</span><span class="special">(</span> <span class="special">\</span>
<span class="identifier">tokens</span> <span class="special">\</span>
<span class="special">)))</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">IS_THIS_BACK</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">this_</span><span class="special">)</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">IS_THIS_BACK</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
<span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
</p>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
<div id="ftn.boost_localfunction.implementation.f0" class="footnote"><p><a href="#boost_localfunction.implementation.f0" class="para"><sup class="para">[31] </sup></a>
This technique is at the core of even more complex preprocessor parsing macros
like the ones that parse the <a href="http://sourceforge.net/projects/contractpp" target="_top">Contract++</a>
syntax.
</p></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Lorenzo
Caminiti<p>
Distributed under the Boost Software License, Version 1.0 (see accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="no_variadic_macros.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../reference.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,265 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Annex: No Variadic Macros</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="prev" href="alternatives.html" title="Annex: Alternatives">
<link rel="next" href="implementation.html" title="Annex: Implementation">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="alternatives.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="implementation.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_localfunction.no_variadic_macros"></a><a class="link" href="no_variadic_macros.html" title="Annex: No Variadic Macros">Annex: No Variadic
Macros</a>
</h2></div></div></div>
<p>
This section illustrates an alternative syntax for compilers without variadic
macro support.
</p>
<h4>
<a name="boost_localfunction.no_variadic_macros.h0"></a>
<span class="phrase"><a name="boost_localfunction.no_variadic_macros.sequence_syntax"></a></span><a class="link" href="no_variadic_macros.html#boost_localfunction.no_variadic_macros.sequence_syntax">Sequence Syntax</a>
</h4>
<p>
Most modern compilers support <a href="http://en.wikipedia.org/wiki/Variadic_macro" target="_top">variaid
macros</a> (notably, these include GCC, MSVC, and all <a href="http://www.open-std.org/JTC1/SC22/WG21/" target="_top">C++11</a>
compilers). However, in the rare case that programmers need to use this library
on a compiler without variadic macros, this library also allows to specify
its macro parameters using a <a href="http://www.boost.org/libs/preprocessor" target="_top">Boost.Preprocessor</a>
sequence where tokens are separated by round parenthesis <code class="computeroutput"><span class="special">()</span></code>:
</p>
<pre class="programlisting"><span class="special">(</span><span class="identifier">token1</span><span class="special">)</span> <span class="special">(</span><span class="identifier">token2</span><span class="special">)</span> <span class="special">...</span> <span class="comment">// All compilers.</span>
</pre>
<p>
Instead of the comma-separated list that we have seen so far which requires
variadic macros:
</p>
<pre class="programlisting"><span class="identifier">token1</span><span class="special">,</span> <span class="identifier">token2</span><span class="special">,</span> <span class="special">...</span> <span class="comment">// Only compilers with varidic macros.</span>
</pre>
<p>
For example, the following syntax is accepted on all compilers with and without
variadic macros (see also <a href="../../../test/add_seq.cpp" target="_top"><code class="literal">add_seq.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">int</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">factor</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span>
<span class="keyword">void</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">factor</span><span class="special">)</span> <span class="special">(</span><span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">sum</span><span class="special">)</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">)</span> <span class="special">{</span>
<span class="identifier">sum</span> <span class="special">+=</span> <span class="identifier">factor</span> <span class="special">*</span> <span class="identifier">num</span><span class="special">;</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">add</span><span class="special">)</span>
<span class="identifier">add</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>
<span class="keyword">int</span> <span class="identifier">nums</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span><span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span><span class="identifier">nums</span><span class="special">,</span> <span class="identifier">nums</span> <span class="special">+</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">add</span><span class="special">);</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">sum</span> <span class="special">==</span> <span class="number">60</span><span class="special">);</span>
<span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
However, on compilers with variadic macros the comma-separated syntax we have
seen so far is preferred because more readable (see also <a href="../../../test/add.cpp" target="_top"><code class="literal">add.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Some local scope.</span>
<span class="keyword">int</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">factor</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span> <span class="comment">// Variables in scope to bind.</span>
<span class="keyword">void</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">factor</span><span class="special">,</span> <span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">sum</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">sum</span> <span class="special">+=</span> <span class="identifier">factor</span> <span class="special">*</span> <span class="identifier">num</span><span class="special">;</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">add</span><span class="special">)</span>
<span class="identifier">add</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="comment">// Call the local function.</span>
<span class="keyword">int</span> <span class="identifier">nums</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span><span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span><span class="identifier">nums</span><span class="special">,</span> <span class="identifier">nums</span> <span class="special">+</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">add</span><span class="special">);</span> <span class="comment">// Pass it to an algorithm.</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">sum</span> <span class="special">==</span> <span class="number">60</span><span class="special">);</span> <span class="comment">// Assert final summation value.</span>
<span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
Note that the same macros accept both syntaxes on compilers with variadic macros
and only the sequence syntax on compilers without variadic macros. Finally,
an empty local function parameter list is always specified using <code class="computeroutput"><span class="keyword">void</span></code> on compilers with and without variadic
macros:
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// No parameter.</span>
<span class="keyword">return</span> <span class="number">10</span><span class="special">;</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">ten</span><span class="special">)</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">ten</span><span class="special">()</span> <span class="special">==</span> <span class="number">10</span><span class="special">);</span>
</pre>
<p>
</p>
<h4>
<a name="boost_localfunction.no_variadic_macros.h1"></a>
<span class="phrase"><a name="boost_localfunction.no_variadic_macros.examples"></a></span><a class="link" href="no_variadic_macros.html#boost_localfunction.no_variadic_macros.examples">Examples</a>
</h4>
<p>
For reference, the following is a list of most of the examples presented in
this documentation reprogrammed using the sequence syntax instead of the comma-separated
syntax (in alphabetic order):
</p>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
<p>
Files
</p>
</th></tr></thead>
<tbody>
<tr><td>
<p>
<a href="../../../test/add_classifiers_seq.cpp" target="_top"><code class="literal">add_classifiers_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/add_default_seq.cpp" target="_top"><code class="literal">add_default_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/add_except_seq.cpp" target="_top"><code class="literal">add_except_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/add_inline_seq.cpp" target="_top"><code class="literal">add_inline_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/add_params_only_seq.cpp" target="_top"><code class="literal">add_params_only_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/add_template_seq.cpp" target="_top"><code class="literal">add_template_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/add_this_seq.cpp" target="_top"><code class="literal">add_this_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/add_typed_seq.cpp" target="_top"><code class="literal">add_typed_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/add_with_default_seq.cpp" target="_top"><code class="literal">add_with_default_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/all_decl_seq.cpp" target="_top"><code class="literal">all_decl_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/factorial_seq.cpp" target="_top"><code class="literal">factorial_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/macro_commas_seq.cpp" target="_top"><code class="literal">macro_commas_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/nesting_seq.cpp" target="_top"><code class="literal">nesting_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/overload_seq.cpp" target="_top"><code class="literal">overload_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/return_assign_seq.cpp" target="_top"><code class="literal">return_assign_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/return_derivative_seq.cpp" target="_top"><code class="literal">return_derivative_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/return_inc_seq.cpp" target="_top"><code class="literal">return_inc_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/return_setget_seq.cpp" target="_top"><code class="literal">return_setget_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/return_this_seq.cpp" target="_top"><code class="literal">return_this_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/same_line_seq.cpp" target="_top"><code class="literal">same_line_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/transform_seq.cpp" target="_top"><code class="literal">transform_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/typeof_seq.cpp" target="_top"><code class="literal">typeof_seq.cpp</code></a>
</p>
</td></tr>
<tr><td>
<p>
<a href="../../../test/typeof_template_seq.cpp" target="_top"><code class="literal">typeof_template_seq.cpp</code></a>
</p>
</td></tr>
</tbody>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Lorenzo
Caminiti<p>
Distributed under the Boost Software License, Version 1.0 (see accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="alternatives.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="implementation.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,168 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Release Notes</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="prev" href="../reference.html" title="Reference">
<link rel="next" href="bibliography.html" title="Bibliography">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../reference.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="bibliography.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_localfunction.release_notes"></a><a class="link" href="release_notes.html" title="Release Notes">Release Notes</a>
</h2></div></div></div>
<p>
This section lists the major changes between different library releases (in
chronological order).
</p>
<h4>
<a name="boost_localfunction.release_notes.h0"></a>
<span class="phrase"><a name="boost_localfunction.release_notes.version_1_0_0__2012_04_12_"></a></span><a class="link" href="release_notes.html#boost_localfunction.release_notes.version_1_0_0__2012_04_12_">Version
1.0.0 (2012-04-12)</a>
</h4>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
Incorporated all comments from the <a href="http://lists.boost.org/boost-announce/2011/12/0340.php" target="_top">Boost
review of this library</a>.
</li>
<li class="listitem">
Removed local blocks and local exits.
</li>
<li class="listitem">
Renamed the library from Boost.Local to Boost.LocalFunction.
</li>
<li class="listitem">
Using <code class="computeroutput"><span class="identifier">this_</span></code> instead of
<code class="computeroutput"><span class="keyword">this</span></code> also in the local function
declaration (not just the body).
</li>
<li class="listitem">
Made changes that allow to return local functions (similar to closures).
</li>
<li class="listitem">
Added GCC lambda and constant block examples.
</li>
<li class="listitem">
Moved <code class="computeroutput"><span class="identifier">overloaded_function</span></code>
to Boost.Functional/OverloadedFunction.
</li>
<li class="listitem">
Moved <code class="computeroutput"><span class="identifier">BOOST_IDENTITY_TYPE</span></code>
to Boost.Utility/IdentityType.
</li>
<li class="listitem">
Completely removed use of Boost.Typeof when bound and result types are
explicitly specified.
</li>
<li class="listitem">
Added <code class="computeroutput"><span class="special">...</span><span class="identifier">_ID</span></code>
macros for multiple expansions on the same line.
</li>
<li class="listitem">
Fixed compilation on Boost regression test platforms.
</li>
</ol></div>
<h4>
<a name="boost_localfunction.release_notes.h1"></a>
<span class="phrase"><a name="boost_localfunction.release_notes.version_0_2_0__2011_05_14_"></a></span><a class="link" href="release_notes.html#boost_localfunction.release_notes.version_0_2_0__2011_05_14_">Version
0.2.0 (2011-05-14)</a>
</h4>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
Replaced parenthesized syntax with variadic and sequencing macro syntaxes.
</li>
<li class="listitem">
Profiled library performances against other approaches.
</li>
<li class="listitem">
Replaced virtual functor trick with casting functor trick (for smaller
run-time).
</li>
<li class="listitem">
Optimized library run-time (rearranging code and not using casting functor
trick on compilers that accept local classes as template parameters).
</li>
<li class="listitem">
Supported inline and recursive local functions.
</li>
<li class="listitem">
Added type-of macro to expose bound types.
</li>
<li class="listitem">
Allowed to explicitly specify bound types.
</li>
<li class="listitem">
Removed using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span></code> instead of exposing internal
local functor as public API.
</li>
<li class="listitem">
Added functor to overload local functions (and functors in general).
</li>
<li class="listitem">
Implemented support for nesting local functions, blocks, and exits into
one another.
</li>
</ol></div>
<h4>
<a name="boost_localfunction.release_notes.h2"></a>
<span class="phrase"><a name="boost_localfunction.release_notes.version_0_1_1__2011_01_10_"></a></span><a class="link" href="release_notes.html#boost_localfunction.release_notes.version_0_1_1__2011_01_10_">Version
0.1.1 (2011-01-10)</a>
</h4>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
Uploaded library source into Boost SVN sandbox.
</li>
<li class="listitem">
Fixed prev/next arrows and other minor layouts in documentation.
</li>
<li class="listitem">
Added Release section to documentation.
</li>
</ol></div>
<h4>
<a name="boost_localfunction.release_notes.h3"></a>
<span class="phrase"><a name="boost_localfunction.release_notes.version_0_1_0__2011_01_03_"></a></span><a class="link" href="release_notes.html#boost_localfunction.release_notes.version_0_1_0__2011_01_03_">Version
0.1.0 (2011-01-03)</a>
</h4>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
Shared with Boost for first round of comments.
</li></ol></div>
<h4>
<a name="boost_localfunction.release_notes.h4"></a>
<span class="phrase"><a name="boost_localfunction.release_notes.version_0_0_1__2010_12_15_"></a></span><a class="link" href="release_notes.html#boost_localfunction.release_notes.version_0_0_1__2010_12_15_">Version
0.0.1 (2010-12-15)</a>
</h4>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
Completed development, examples, and documentation.
</li></ol></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Lorenzo
Caminiti<p>
Distributed under the Boost Software License, Version 1.0 (see accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../reference.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="bibliography.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,540 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Tutorial</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.LocalFunction 1.0.0">
<link rel="prev" href="getting_started.html" title="Getting Started">
<link rel="next" href="advanced_topics.html" title="Advanced Topics">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="getting_started.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="advanced_topics.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_localfunction.tutorial"></a><a class="link" href="tutorial.html" title="Tutorial">Tutorial</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="tutorial.html#boost_localfunction.tutorial.local_functions">Local Functions</a></span></dt>
<dt><span class="section"><a href="tutorial.html#boost_localfunction.tutorial.Binding">Binding Variables</a></span></dt>
<dt><span class="section"><a href="tutorial.html#boost_localfunction.tutorial.binding_the_object__this_">Binding
the Object <code class="computeroutput"><span class="keyword">this</span></code></a></span></dt>
<dt><span class="section"><a href="tutorial.html#boost_localfunction.tutorial.templates">Templates</a></span></dt>
</dl></div>
<p>
This section illustrates basic usage of this library.
</p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.tutorial.local_functions"></a><a class="link" href="tutorial.html#boost_localfunction.tutorial.local_functions" title="Local Functions">Local Functions</a>
</h3></div></div></div>
<p>
Local functions are defined using macros from the header file <code class="computeroutput">boost/local_function.hpp</code>. The
macros must be used from within a declarative context (this is a limitation
with respect to <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> which can instead be declared also within expressions):
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">local_function</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// This library header.</span>
<span class="special">...</span>
<span class="special">{</span> <span class="comment">// Some declarative context.</span>
<span class="special">...</span>
<span class="emphasis"><em>result-type</em></span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="emphasis"><em>parameters</em></span><span class="special">)</span> <span class="special">{</span>
<span class="emphasis"><em>body-code</em></span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="emphasis"><em>name</em></span><span class="special">)</span>
<span class="special">...</span>
<span class="special">}</span>
</pre>
<p>
The code expanded by the macros declares a function object (or <a href="http://en.wikipedia.org/wiki/Functor" target="_top">functor</a>)
with the local function name specified by <code class="computeroutput">BOOST_LOCAL_FUNCTION_NAME</code>.
<a href="#ftn.boost_localfunction.tutorial.local_functions.f0" class="footnote" name="boost_localfunction.tutorial.local_functions.f0"><sup class="footnote">[5]</sup></a> The usual C++ scope visibility rules apply to local functions
for which a local function is visible only within the enclosing scope in
which it is declared.
</p>
<p>
The local function result type is specified just before the <code class="computeroutput">BOOST_LOCAL_FUNCTION</code>
macro.
</p>
<p>
The local function body is specified using the usual C++ statement syntax
in a code block <code class="computeroutput"><span class="special">{</span> <span class="special">...</span>
<span class="special">}</span></code> between the <code class="computeroutput">BOOST_LOCAL_FUNCTION</code>
and <code class="computeroutput">BOOST_LOCAL_FUNCTION_NAME</code>
macros. The body is specified outside any of the macros so eventual compiler
error messages and related line numbers retain their usual meaning and format.
<a href="#ftn.boost_localfunction.tutorial.local_functions.f1" class="footnote" name="boost_localfunction.tutorial.local_functions.f1"><sup class="footnote">[6]</sup></a>
</p>
<p>
The local function parameters are passed to the <code class="computeroutput">BOOST_LOCAL_FUNCTION</code>
macro as a comma-separated list of tokens (see the <a class="link" href="no_variadic_macros.html" title="Annex: No Variadic Macros">No
Variadic Macros</a> section for compilers that do not support variadic
macros):
</p>
<pre class="programlisting"><span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><code class="literal"><span class="emphasis"><em>parameter-type1 parameter-name1</em></span></code><span class="special">,</span> <code class="literal"><span class="emphasis"><em>parameter-type2 parameter-name2, ...</em></span></code><span class="special">)</span>
</pre>
<p>
The maximum number of parameters that can be passed to a local function is
controlled at compile-time by the configuration macro <code class="computeroutput">BOOST_LOCAL_FUNCTION_CONFIG_ARITY_MAX</code>.
For example, let's program a local function named <code class="computeroutput"><span class="identifier">add</span></code>
that adds together two integers <code class="computeroutput"><span class="identifier">x</span></code>
and <code class="computeroutput"><span class="identifier">y</span></code> (see also <a href="../../../test/add_params_only.cpp" target="_top"><code class="literal">add_params_only.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Local function.</span>
<span class="keyword">return</span> <span class="identifier">x</span> <span class="special">+</span> <span class="identifier">y</span><span class="special">;</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">add</span><span class="special">)</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">add</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">)</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span> <span class="comment">// Local function call.</span>
</pre>
<p>
</p>
<p>
If the local function has no parameter, it is possible to pass <code class="computeroutput"><span class="keyword">void</span></code> to the <code class="computeroutput">BOOST_LOCAL_FUNCTION</code>
macro (similarly to the C++ syntax that allows to use <code class="literal"><span class="emphasis"><em>result-type
function-name</em></span></code><code class="computeroutput"><span class="special">(</span><span class="keyword">void</span><span class="special">)</span></code> to declare
a function with no parameter): <a href="#ftn.boost_localfunction.tutorial.local_functions.f2" class="footnote" name="boost_localfunction.tutorial.local_functions.f2"><sup class="footnote">[7]</sup></a>
</p>
<pre class="programlisting"><span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="comment">// No parameter.</span>
</pre>
<p>
For example, let's program a local function that always returns <code class="computeroutput"><span class="number">10</span></code> (see also <a href="../../../test/ten_void.cpp" target="_top"><code class="literal">ten_void.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// No parameter.</span>
<span class="keyword">return</span> <span class="number">10</span><span class="special">;</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">ten</span><span class="special">)</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">ten</span><span class="special">()</span> <span class="special">==</span> <span class="number">10</span><span class="special">);</span>
</pre>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.tutorial.Binding"></a><a class="link" href="tutorial.html#boost_localfunction.tutorial.Binding" title="Binding Variables">Binding Variables</a>
</h3></div></div></div>
<p>
Variables in scope (local variables, enclosing function parameters, data
members, etc) can be bound to a local function declaration. Only bound variables,
static variables, global variables, functions, and enumerations from the
enclosing scope are accessible from within the local function body. The types
of bound variables are deduced automatically by this library using <a href="http://www.boost.org/libs/typeof" target="_top">Boost.Typeof</a>. <a href="#ftn.boost_localfunction.tutorial.Binding.f0" class="footnote" name="boost_localfunction.tutorial.Binding.f0"><sup class="footnote">[8]</sup></a>
</p>
<p>
This library introduces the new "keyword" <code class="computeroutput"><span class="identifier">bind</span></code>
<a href="#ftn.boost_localfunction.tutorial.Binding.f1" class="footnote" name="boost_localfunction.tutorial.Binding.f1"><sup class="footnote">[9]</sup></a> which is used in place of the parameter type to specify the name
of a variable in scope to bind (therefore, <code class="computeroutput"><span class="identifier">bind</span></code>
cannot be used as a local function parameter type). A variable can be bound
by value:
</p>
<pre class="programlisting"><span class="identifier">bind</span> <span class="emphasis"><em>variable-name</em></span> <span class="comment">// Bind by value.</span>
</pre>
<p>
Or by reference prefixing the variable name with <code class="computeroutput"><span class="special">&amp;</span></code>:
</p>
<pre class="programlisting"><span class="identifier">bind</span><span class="special">&amp;</span> <span class="emphasis"><em>variable-name</em></span> <span class="comment">// Bind by reference.</span>
</pre>
<p>
Furthermore, the "keyword" <code class="computeroutput"><span class="identifier">bind</span></code>
can be prefixed by <code class="computeroutput"><span class="keyword">const</span></code> to
bind the variable by constant value:
</p>
<pre class="programlisting"><span class="keyword">const</span> <span class="identifier">bind</span> <span class="emphasis"><em>variable-name</em></span> <span class="comment">// Bind by constant value.</span>
</pre>
<p>
Or by constant reference:
</p>
<pre class="programlisting"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&amp;</span> <span class="emphasis"><em>variable-name</em></span> <span class="comment">// Bind by constant value.</span>
</pre>
<p>
Note that when <code class="computeroutput"><span class="keyword">const</span></code> is used,
it must always precede <code class="computeroutput"><span class="identifier">bind</span></code>.
<a href="#ftn.boost_localfunction.tutorial.Binding.f2" class="footnote" name="boost_localfunction.tutorial.Binding.f2"><sup class="footnote">[10]</sup></a>
</p>
<p>
If a variable is bound by value, then a copy of the variable value is taken
at the point of the local function declaration. If a variable is bound by
reference instead, the variable will refer to the value it has at the point
of the local function call. Furthermore, it is the programmers' responsibility
to ensure that variables bound by reference survive the existence scope of
the local function otherwise the bound references will be invalid when the
local function is called resulting in undefined behaviour (in other words,
the usual care in using C++ references must be taken for variables bound
by reference).
</p>
<p>
The type of a bound variable is automatically deduced using <a href="http://www.boost.org/libs/typeof" target="_top">Boost.Typeof</a>
and it is the exact same type used to declare such a variable in the enclosing
scope with the following notes:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
If a bound variable was declared constant in the enclosing scope, it
will always be bound by constant value or constant reference even if
<code class="computeroutput"><span class="identifier">bind</span><span class="special">...</span></code>
is used instead of <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">...</span></code>
. However, if a bound variable was not declared constant in the enclosing
scope then it will not be bound as constant unless constant binding is
forced using <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">...</span></code>. (Note that binding by constant reference
is not supported by <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11
lambda functions</a> but it is supported by this library.) <a href="#ftn.boost_localfunction.tutorial.Binding.f3" class="footnote" name="boost_localfunction.tutorial.Binding.f3"><sup class="footnote">[11]</sup></a>
</li>
<li class="listitem">
If a bound variable was declared as a reference in the enclosing scope,
it will still be bound by value unless it is explicitly bound by reference
using <code class="computeroutput"><span class="identifier">bind</span><span class="special">&amp;</span></code>
or <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&amp;</span></code>. <a href="#ftn.boost_localfunction.tutorial.Binding.f4" class="footnote" name="boost_localfunction.tutorial.Binding.f4"><sup class="footnote">[12]</sup></a>
</li>
</ul></div>
<p>
When a variable is bound by value (constant or not), its type must be <a href="http://www.boost.org/doc/libs/release/doc/html/CopyConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">CopyConstructible</span></code></a> (i.e., its must
provide a copy constructor). As with passing parameters to usual C++ functions,
programmers might want to bind variables of complex types by (possibly constant)
reference instead of by value to avoid expensive copy operations when these
variables are bound to a local function.
</p>
<p>
For example, let's program the local function <code class="computeroutput"><span class="identifier">add</span></code>
from the example in the <a class="link" href="../index.html#boost_localfunction.introduction" title="Introduction">Introduction</a>
section. We bind the local variable <code class="computeroutput"><span class="identifier">factor</span></code>
by constant value (because its value should not be modified by the local
function), the local variable <code class="computeroutput"><span class="identifier">sum</span></code>
by non-constant reference (because its value needs to be updated with the
summation result), and program the body to perform the summation (see also
<a href="../../../test/add.cpp" target="_top"><code class="literal">add.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="special">{</span> <span class="comment">// Some local scope.</span>
<span class="keyword">int</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">factor</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span> <span class="comment">// Variables in scope to bind.</span>
<span class="keyword">void</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">factor</span><span class="special">,</span> <span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">sum</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">sum</span> <span class="special">+=</span> <span class="identifier">factor</span> <span class="special">*</span> <span class="identifier">num</span><span class="special">;</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">add</span><span class="special">)</span>
<span class="identifier">add</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="comment">// Call the local function.</span>
<span class="keyword">int</span> <span class="identifier">nums</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span><span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span><span class="identifier">nums</span><span class="special">,</span> <span class="identifier">nums</span> <span class="special">+</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">add</span><span class="special">);</span> <span class="comment">// Pass it to an algorithm.</span>
<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">sum</span> <span class="special">==</span> <span class="number">60</span><span class="special">);</span> <span class="comment">// Assert final summation value.</span>
<span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.tutorial.binding_the_object__this_"></a><a class="link" href="tutorial.html#boost_localfunction.tutorial.binding_the_object__this_" title="Binding the Object this">Binding
the Object <code class="computeroutput"><span class="keyword">this</span></code></a>
</h3></div></div></div>
<p>
It is also possible to bind the object <code class="computeroutput"><span class="keyword">this</span></code>
when it is in scope (e.g., from an enclosing non-static member function).
This is done by using the special symbol <code class="computeroutput"><span class="identifier">this_</span></code>
(instead of <code class="computeroutput"><span class="keyword">this</span></code>) as the name
of the variable to bind in the local function declaration and also to access
the object within the local function body. <a href="#ftn.boost_localfunction.tutorial.binding_the_object__this_.f0" class="footnote" name="boost_localfunction.tutorial.binding_the_object__this_.f0"><sup class="footnote">[13]</sup></a>
</p>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
The library will generate a compile-time error if <code class="computeroutput"><span class="keyword">this</span></code>
is mistakenly used instead of <code class="computeroutput"><span class="identifier">this_</span></code>
to bind the object in the local function declaration. However, mistakenly
using <code class="computeroutput"><span class="keyword">this</span></code> instead of <code class="computeroutput"><span class="identifier">this_</span></code> to access the object within the
local function body will leads to undefined behaviour and it will not necessarily
generate a compile-time error. <a href="#ftn.boost_localfunction.tutorial.binding_the_object__this_.f1" class="footnote" name="boost_localfunction.tutorial.binding_the_object__this_.f1"><sup class="footnote">[14]</sup></a> Programmers are ultimately responsible to make sure that <code class="computeroutput"><span class="keyword">this</span></code> is never used within a local function.
</p></td></tr>
</table></div>
<p>
The object <code class="computeroutput"><span class="keyword">this</span></code> can be bound
by value:
</p>
<pre class="programlisting"><span class="identifier">bind</span> <span class="identifier">this_</span> <span class="comment">// Bind the object `this` by value.</span>
</pre>
<p>
In this case the local function will be able to modify the object when the
enclosing scope is not a constant member and it will not be able to modify
the object when the enclosing scope is a constant member. Otherwise, the
object <code class="computeroutput"><span class="keyword">this</span></code> can be bound by
constant value:
</p>
<pre class="programlisting"><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">this_</span> <span class="comment">// Bind the object `this` by constant value.</span>
</pre>
<p>
In this case the local function will never be able to modify the object (regardless
of whether the enclosing scope is a constant member or not).
</p>
<p>
Note that the object <code class="computeroutput"><span class="keyword">this</span></code> can
never be bound by reference because C++ does not allow to obtain a reference
to <code class="computeroutput"><span class="keyword">this</span></code> (the library will generate
a compile-time error if programmers try to use <code class="computeroutput"><span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">this_</span></code>
or <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">this_</span></code>).
Note that <code class="computeroutput"><span class="keyword">this</span></code> is a pointer
so the pointed object is never copied even if <code class="computeroutput"><span class="keyword">this</span></code>
is bound by value (also it is not possible to directly bind <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> because
<code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
is an expression and not a variable name).
</p>
<p>
For example, let's program a local function <code class="computeroutput"><span class="identifier">add</span></code>
similar to the one in the example from the <a class="link" href="../index.html#boost_localfunction.introduction" title="Introduction">Introduction</a>
section but using a member function to illustrate how to bind the object
<code class="computeroutput"><span class="keyword">this</span></code> (see also <a href="../../../test/add_this.cpp" target="_top"><code class="literal">add_this.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">adder</span> <span class="special">{</span>
<span class="identifier">adder</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">sum_</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special">{}</span>
<span class="keyword">int</span> <span class="identifier">sum</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;&amp;</span> <span class="identifier">nums</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">factor</span> <span class="special">=</span> <span class="number">10</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">void</span> <span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">factor</span><span class="special">,</span> <span class="identifier">bind</span> <span class="identifier">this_</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">this_</span><span class="special">-&gt;</span><span class="identifier">sum_</span> <span class="special">+=</span> <span class="identifier">factor</span> <span class="special">*</span> <span class="identifier">num</span><span class="special">;</span> <span class="comment">// Use `this_` instead of `this`.</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME</span><span class="special">(</span><span class="identifier">add</span><span class="special">)</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span><span class="identifier">nums</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">nums</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">add</span><span class="special">);</span>
<span class="keyword">return</span> <span class="identifier">sum_</span><span class="special">;</span>
<span class="special">}</span>
<span class="keyword">private</span><span class="special">:</span>
<span class="keyword">int</span> <span class="identifier">sum_</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
</p>
<p>
Note that the local function has access to all class members via the bound
object <code class="computeroutput"><span class="identifier">this_</span></code> regardless of
their access level (<code class="computeroutput"><span class="keyword">public</span></code>,
<code class="computeroutput"><span class="keyword">protected</span></code>, or <code class="computeroutput"><span class="keyword">private</span></code>). <a href="#ftn.boost_localfunction.tutorial.binding_the_object__this_.f2" class="footnote" name="boost_localfunction.tutorial.binding_the_object__this_.f2"><sup class="footnote">[15]</sup></a> Specifically, in the example above the local function updates
the private data member <code class="computeroutput"><span class="identifier">sum_</span></code>.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_localfunction.tutorial.templates"></a><a class="link" href="tutorial.html#boost_localfunction.tutorial.templates" title="Templates">Templates</a>
</h3></div></div></div>
<p>
When local functions are programmed within templates, they need to be declared
using the special macros <code class="computeroutput">BOOST_LOCAL_FUNCTION_TPL</code>
and <code class="computeroutput">BOOST_LOCAL_FUNCTION_NAME_TPL</code>:
<a href="#ftn.boost_localfunction.tutorial.templates.f0" class="footnote" name="boost_localfunction.tutorial.templates.f0"><sup class="footnote">[16]</sup></a>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">local_function</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// This library header.</span>
<span class="special">...</span>
<span class="special">{</span> <span class="comment">// Some declarative context within a template.</span>
<span class="special">...</span>
<span class="emphasis"><em>result-type</em></span> <span class="identifier">BOOST_LOCAL_FUNCTION_TPL</span><span class="special">(</span><span class="emphasis"><em>parameters</em></span><span class="special">)</span> <span class="special">{</span>
<span class="emphasis"><em>body-code</em></span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME_TPL</span><span class="special">(</span><span class="emphasis"><em>name</em></span><span class="special">)</span>
<span class="special">...</span>
<span class="special">}</span>
</pre>
<p>
The <code class="computeroutput">BOOST_LOCAL_FUNCTION_TPL</code>
and <code class="computeroutput">BOOST_LOCAL_FUNCTION_NAME_TPL</code>
macros have the exact same syntax of the <code class="computeroutput">BOOST_LOCAL_FUNCTION</code>
and <code class="computeroutput">BOOST_LOCAL_FUNCTION_NAME</code>
macros that we have seen so far.
</p>
<p>
For example, let's program a local function similar to the one from the
<a class="link" href="../index.html#boost_localfunction.introduction" title="Introduction">Introduction</a> section
but within a template (see also <a href="../../../test/add_template.cpp" target="_top"><code class="literal">add_template.cpp</code></a>):
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">T</span> <span class="identifier">total</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">y</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">z</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">T</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">(),</span> <span class="identifier">factor</span> <span class="special">=</span> <span class="number">10</span><span class="special">;</span>
<span class="comment">// Must use the `..._TPL` macros within templates.</span>
<span class="identifier">T</span> <span class="identifier">BOOST_LOCAL_FUNCTION_TPL</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">bind</span> <span class="identifier">factor</span><span class="special">,</span> <span class="identifier">bind</span><span class="special">&amp;</span> <span class="identifier">sum</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">num</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">return</span> <span class="identifier">sum</span> <span class="special">+=</span> <span class="identifier">factor</span> <span class="special">*</span> <span class="identifier">num</span><span class="special">;</span>
<span class="special">}</span> <span class="identifier">BOOST_LOCAL_FUNCTION_NAME_TPL</span><span class="special">(</span><span class="identifier">add</span><span class="special">)</span>
<span class="identifier">add</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span>
<span class="identifier">T</span> <span class="identifier">nums</span><span class="special">[</span><span class="number">2</span><span class="special">];</span> <span class="identifier">nums</span><span class="special">[</span><span class="number">0</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">y</span><span class="special">;</span> <span class="identifier">nums</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">=</span> <span class="identifier">z</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span><span class="identifier">nums</span><span class="special">,</span> <span class="identifier">nums</span> <span class="special">+</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">add</span><span class="special">);</span>
<span class="keyword">return</span> <span class="identifier">sum</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
</p>
</div>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
<div id="ftn.boost_localfunction.tutorial.local_functions.f0" class="footnote"><p><a href="#boost_localfunction.tutorial.local_functions.f0" class="para"><sup class="para">[5] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> The local function name must
be passed to the macro <code class="computeroutput">BOOST_LOCAL_FUNCTION_NAME</code>
ending the function definition so this macro can declare a local variable
with the local function name to hold the local function object. Therefore
the local function name cannot be specified within the <code class="computeroutput">BOOST_LOCAL_FUNCTION</code>
and it must appear instead after the local function body (even if that
differs from the usual C++ function declaration syntax).
</p></div>
<div id="ftn.boost_localfunction.tutorial.local_functions.f1" class="footnote"><p><a href="#boost_localfunction.tutorial.local_functions.f1" class="para"><sup class="para">[6] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> If the local function body
were instead passed as a macro parameter, it would be expanded on a single
line of code (because macros always expand as a single line of code). Therefore,
eventual compiler error line numbers would all report the same value and
would no longer be useful to pinpoint errors.
</p></div>
<div id="ftn.boost_localfunction.tutorial.local_functions.f2" class="footnote"><p><a href="#boost_localfunction.tutorial.local_functions.f2" class="para"><sup class="para">[7] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> The <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/standards" target="_top">C++03</a>
standard does not allow to pass empty parameters to a macro so the macro
cannot be invoked as <code class="computeroutput"><span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">()</span></code>. On <a href="http://www.open-std.org/jtc1/sc22/wg14/www/projects#9899" target="_top">C99</a>
compilers with properly implemented empty macro parameter support, it would
be possible to allow <code class="computeroutput"><span class="identifier">BOOST_LOCAL_FUNCTION</span><span class="special">()</span></code> but this is already not the case for
MSVC so this syntax is never allowed to ensure better portability.
</p></div>
<div id="ftn.boost_localfunction.tutorial.Binding.f0" class="footnote"><p><a href="#boost_localfunction.tutorial.Binding.f0" class="para"><sup class="para">[8] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> By binding a variable in scope,
the local function declaration is specifying that such a variable should
be accessible within the local function body regardless of its type. Semantically,
this binding should be seen as an "extension" of the scope of
the bound variable from the enclosing scope to the scope of the local function
body. Therefore, contrary to the semantic of passing a function parameter,
the semantic of binding a variable does not depend on the variable type
but just on the variable name: "The variable in scope named <span class="emphasis"><em>x</em></span>
should be accessible within the local function named <span class="emphasis"><em>f</em></span>".
For example, this reduces maintenance because if a bound variable type
is changed, the local function declaration does not have to change.
</p></div>
<div id="ftn.boost_localfunction.tutorial.Binding.f1" class="footnote"><p><a href="#boost_localfunction.tutorial.Binding.f1" class="para"><sup class="para">[9] </sup></a>
Obviously, the token <code class="computeroutput"><span class="identifier">bind</span></code>
is not a keyword of the C++ language. This library parses the token <code class="computeroutput"><span class="identifier">bind</span></code> during macro expansion using preprocessor
meta-programming (see the <a class="link" href="implementation.html" title="Annex: Implementation">Implementation</a>
section). Therefore, <code class="computeroutput"><span class="identifier">bind</span></code>
can be considered a new "keyword" only at the preprocessor meta-programming
level within the syntax defined by the macros of this library (thus it
is referred to as a "keyword" only within quotes).
</p></div>
<div id="ftn.boost_localfunction.tutorial.Binding.f2" class="footnote"><p><a href="#boost_localfunction.tutorial.Binding.f2" class="para"><sup class="para">[10] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> The library macros could have
been implemented to accept both syntaxes <code class="computeroutput"><span class="keyword">const</span>
<span class="identifier">bind</span> <span class="special">...</span></code>
and <code class="computeroutput"><span class="identifier">bind</span> <span class="keyword">const</span>
<span class="special">...</span></code> equivalently. However, handling
both syntaxes would have complicated the macro implementation without adding
any feature so only one syntax <code class="computeroutput"><span class="keyword">const</span>
<span class="identifier">bind</span> <span class="special">...</span></code>
is supported.
</p></div>
<div id="ftn.boost_localfunction.tutorial.Binding.f3" class="footnote"><p><a href="#boost_localfunction.tutorial.Binding.f3" class="para"><sup class="para">[11] </sup></a>
An historical note: Constant binding of variables in scope was the
main use case that originally motivated the authors in developing this
library. The authors needed to locally create a chuck of code to assert
some correctness conditions while these assertions were not supposed
to modify any of the variables they were using (see the <a href="http://sourceforge.net/projects/contractpp" target="_top">Contract++</a>
library). This was achieved by binding by constant reference <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&amp;</span></code> the variables needed by the assertions
and then by programming the local function body to check the assertions.
This way if any of the assertions mistakenly changes a bound variable
(for example confusing the operator <code class="computeroutput"><span class="special">==</span></code>
with <code class="computeroutput"><span class="special">=</span></code>), the compiler
correctly generates an error because the bound variable is of <code class="computeroutput"><span class="keyword">const</span></code> type within the local function
body (see also <span class="emphasis"><em>constant blocks</em></span> in the <a class="link" href="examples.html" title="Examples">Examples</a>
section).
</p></div>
<div id="ftn.boost_localfunction.tutorial.Binding.f4" class="footnote"><p><a href="#boost_localfunction.tutorial.Binding.f4" class="para"><sup class="para">[12] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> Variables originally declared
as references are bound by value unless <code class="computeroutput"><span class="special">[</span><span class="keyword">const</span><span class="special">]</span> <span class="identifier">bind</span><span class="special">&amp;</span></code>
is used so that references can be bound by both value <code class="computeroutput"><span class="special">[</span><span class="keyword">const</span><span class="special">]</span> <span class="identifier">bind</span></code>
and reference <code class="computeroutput"><span class="special">[</span><span class="keyword">const</span><span class="special">]</span> <span class="identifier">bind</span><span class="special">&amp;</span></code> (this is the same binding semantic
adopted by <a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a>).
However, variables originally declared as constants should never loose
their <code class="computeroutput"><span class="keyword">const</span></code> qualifier
(to prevent their modification not just in the enclosing scope but
also in the local scope) thus they are always bound by constant even
if <code class="computeroutput"><span class="identifier">bind</span><span class="special">[&amp;]</span></code>
is used instead of <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">[&amp;]</span></code>.
</p></div>
<div id="ftn.boost_localfunction.tutorial.binding_the_object__this_.f0" class="footnote"><p><a href="#boost_localfunction.tutorial.binding_the_object__this_.f0" class="para"><sup class="para">[13] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> The special name <code class="computeroutput"><span class="identifier">this_</span></code> was chosen following <a href="http://lists.boost.org/Archives/boost/2011/04/179729.php" target="_top">Boost
practise</a> to postfix with an underscore identifiers that are named
after keywords (the C++ keyword <code class="computeroutput"><span class="keyword">this</span></code>
in this case). The special symbol <code class="computeroutput"><span class="identifier">this_</span></code>
is needed because <code class="computeroutput"><span class="keyword">this</span></code> is
a reserved C++ keyword so it cannot be used as the name of the internal
parameter that passes the bound object to the local function body. It would
have been possible to use <code class="computeroutput"><span class="keyword">this</span></code>
(instead of <code class="computeroutput"><span class="identifier">this_</span></code>) within
the local function body either at the expenses of copying the bound object
(which would introduce run-time overhead and also the stringent requirement
that the bound object must have a deep copy constructor) or by relying
on an <a href="http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/d3a86f27277f713b" target="_top">undefined
behaviour of <code class="computeroutput"><span class="keyword">static_cast</span></code></a>
(which might not work on all platforms at the cost of portability).
</p></div>
<div id="ftn.boost_localfunction.tutorial.binding_the_object__this_.f1" class="footnote"><p><a href="#boost_localfunction.tutorial.binding_the_object__this_.f1" class="para"><sup class="para">[14] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> The local function body cannot
be a static member function of the local functor object in order to support
recursion (because the local function name is specified by the <code class="computeroutput">BOOST_LOCAL_FUNCTION_NAME</code>
macro only after the body so it must be made available via a functor
data member named after the local function and local classes cannot have
static data members in C++) and nesting (because the argument binding
variable must be declared as a data member so it is visible in a local
function nested within the body member function) -- see the <a class="link" href="implementation.html" title="Annex: Implementation">Implementation</a>
section. Therefore, from within the local function body the variable
<code class="computeroutput"><span class="keyword">this</span></code> is visible but it refers
to the local functor and not to the bound object.
</p></div>
<div id="ftn.boost_localfunction.tutorial.binding_the_object__this_.f2" class="footnote"><p><a href="#boost_localfunction.tutorial.binding_the_object__this_.f2" class="para"><sup class="para">[15] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> This is possible because of
the fix to C++ <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#45" target="_top">defect
45</a> that made inner and local types able to access all outer class
members regardless of their access level.
</p></div>
<div id="ftn.boost_localfunction.tutorial.templates.f0" class="footnote"><p><a href="#boost_localfunction.tutorial.templates.f0" class="para"><sup class="para">[16] </sup></a>
<span class="bold"><strong>Rationale.</strong></span> Within templates, this library
needs to use <code class="computeroutput"><span class="keyword">typename</span></code> to explicitly
indicate that some expressions evaluate to a type. Because <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/standards" target="_top">C++03</a>
does not allow to use <code class="computeroutput"><span class="keyword">typename</span></code>
outside templates, the special <code class="computeroutput"><span class="special">...</span><span class="identifier">_TPL</span></code> macros are used to indicate that
the enclosing scope is a template so this library can safely use <code class="computeroutput"><span class="keyword">typename</span></code> to resolve expression type ambiguities.
<a href="http://www.open-std.org/JTC1/SC22/WG21/" target="_top">C++11</a> and
other compilers might compile local functions within templates even when
the <code class="computeroutput"><span class="special">...</span><span class="identifier">_TPL</span></code>
macros are not used. However, it is recommended to always use the <code class="computeroutput"><span class="special">...</span><span class="identifier">_TPL</span></code>
macros within templates to maximize portability.
</p></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2012 Lorenzo
Caminiti<p>
Distributed under the Boost Software License, Version 1.0 (see accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="getting_started.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="advanced_topics.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>