]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Tutorial</title> | |
5 | <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.78.1"> | |
7 | <link rel="home" href="../index.html" title="Chapter 1. Boost.LocalFunction 1.0.0"> | |
8 | <link rel="up" href="../index.html" title="Chapter 1. Boost.LocalFunction 1.0.0"> | |
9 | <link rel="prev" href="getting_started.html" title="Getting Started"> | |
10 | <link rel="next" href="advanced_topics.html" title="Advanced Topics"> | |
11 | </head> | |
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> | |
13 | <table cellpadding="2" width="100%"><tr> | |
14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> | |
15 | <td align="center"><a href="../../../../../index.html">Home</a></td> | |
16 | <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> | |
17 | <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> | |
18 | <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> | |
19 | <td align="center"><a href="../../../../../more/index.htm">More</a></td> | |
20 | </tr></table> | |
21 | <hr> | |
22 | <div class="spirit-nav"> | |
23 | <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> | |
24 | </div> | |
25 | <div class="section"> | |
26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> | |
27 | <a name="boost_localfunction.tutorial"></a><a class="link" href="tutorial.html" title="Tutorial">Tutorial</a> | |
28 | </h2></div></div></div> | |
29 | <div class="toc"><dl class="toc"> | |
30 | <dt><span class="section"><a href="tutorial.html#boost_localfunction.tutorial.local_functions">Local Functions</a></span></dt> | |
31 | <dt><span class="section"><a href="tutorial.html#boost_localfunction.tutorial.Binding">Binding Variables</a></span></dt> | |
32 | <dt><span class="section"><a href="tutorial.html#boost_localfunction.tutorial.binding_the_object__this_">Binding | |
33 | the Object <code class="computeroutput"><span class="keyword">this</span></code></a></span></dt> | |
34 | <dt><span class="section"><a href="tutorial.html#boost_localfunction.tutorial.templates">Templates</a></span></dt> | |
35 | </dl></div> | |
36 | <p> | |
37 | This section illustrates basic usage of this library. | |
38 | </p> | |
39 | <div class="section"> | |
40 | <div class="titlepage"><div><div><h3 class="title"> | |
41 | <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> | |
42 | </h3></div></div></div> | |
43 | <p> | |
44 | Local functions are defined using macros from the header file <code class="computeroutput"><a class="link" href="../reference.html#header.boost.local_function_hpp" title="Header <boost/local_function.hpp>">boost/local_function.hpp</a></code>. The | |
45 | macros must be used from within a declarative context (this is a limitation | |
46 | with respect to <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11 | |
47 | lambda functions</a> which can instead be declared also within expressions): | |
48 | </p> | |
49 | <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">></span> <span class="comment">// This library header.</span> | |
50 | ||
51 | <span class="special">...</span> | |
52 | <span class="special">{</span> <span class="comment">// Some declarative context.</span> | |
53 | <span class="special">...</span> | |
54 | <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> | |
55 | <span class="emphasis"><em>body-code</em></span> | |
56 | <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> | |
57 | <span class="special">...</span> | |
58 | <span class="special">}</span> | |
59 | </pre> | |
60 | <p> | |
61 | The code expanded by the macros declares a function object (or <a href="http://en.wikipedia.org/wiki/Functor" target="_top">functor</a>) | |
62 | with the local function name specified by <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_NAME.html" title="Macro BOOST_LOCAL_FUNCTION_NAME">BOOST_LOCAL_FUNCTION_NAME</a></code>. | |
63 | <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 | |
64 | for which a local function is visible only within the enclosing scope in | |
65 | which it is declared. | |
66 | </p> | |
67 | <p> | |
68 | The local function result type is specified just before the <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION.html" title="Macro BOOST_LOCAL_FUNCTION">BOOST_LOCAL_FUNCTION</a></code> | |
69 | macro. | |
70 | </p> | |
71 | <p> | |
72 | The local function body is specified using the usual C++ statement syntax | |
73 | in a code block <code class="computeroutput"><span class="special">{</span> <span class="special">...</span> | |
74 | <span class="special">}</span></code> between the <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION.html" title="Macro BOOST_LOCAL_FUNCTION">BOOST_LOCAL_FUNCTION</a></code> | |
75 | and <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_NAME.html" title="Macro BOOST_LOCAL_FUNCTION_NAME">BOOST_LOCAL_FUNCTION_NAME</a></code> | |
76 | macros. The body is specified outside any of the macros so eventual compiler | |
77 | error messages and related line numbers retain their usual meaning and format. | |
78 | <a href="#ftn.boost_localfunction.tutorial.local_functions.f1" class="footnote" name="boost_localfunction.tutorial.local_functions.f1"><sup class="footnote">[6]</sup></a> | |
79 | </p> | |
80 | <p> | |
81 | The local function parameters are passed to the <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION.html" title="Macro BOOST_LOCAL_FUNCTION">BOOST_LOCAL_FUNCTION</a></code> | |
82 | macro as a comma-separated list of tokens (see the <a class="link" href="no_variadic_macros.html" title="Annex: No Variadic Macros">No | |
83 | Variadic Macros</a> section for compilers that do not support variadic | |
84 | macros): | |
85 | </p> | |
86 | <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> | |
87 | </pre> | |
88 | <p> | |
89 | The maximum number of parameters that can be passed to a local function is | |
90 | controlled at compile-time by the configuration macro <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_CONFIG_ARITY_MAX.html" title="Macro BOOST_LOCAL_FUNCTION_CONFIG_ARITY_MAX">BOOST_LOCAL_FUNCTION_CONFIG_ARITY_MAX</a></code>. | |
91 | For example, let's program a local function named <code class="computeroutput"><span class="identifier">add</span></code> | |
92 | that adds together two integers <code class="computeroutput"><span class="identifier">x</span></code> | |
93 | 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>): | |
94 | </p> | |
95 | <p> | |
96 | </p> | |
97 | <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> | |
98 | <span class="keyword">return</span> <span class="identifier">x</span> <span class="special">+</span> <span class="identifier">y</span><span class="special">;</span> | |
99 | <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> | |
100 | ||
101 | <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> | |
102 | </pre> | |
103 | <p> | |
104 | </p> | |
105 | <p> | |
106 | 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"><a class="link" href="../BOOST_LOCAL_FUNCTION.html" title="Macro BOOST_LOCAL_FUNCTION">BOOST_LOCAL_FUNCTION</a></code> | |
107 | macro (similarly to the C++ syntax that allows to use <code class="literal"><span class="emphasis"><em>result-type | |
108 | function-name</em></span></code><code class="computeroutput"><span class="special">(</span><span class="keyword">void</span><span class="special">)</span></code> to declare | |
109 | 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> | |
110 | </p> | |
111 | <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> | |
112 | </pre> | |
113 | <p> | |
114 | 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>): | |
115 | </p> | |
116 | <p> | |
117 | </p> | |
118 | <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> | |
119 | <span class="keyword">return</span> <span class="number">10</span><span class="special">;</span> | |
120 | <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> | |
121 | ||
122 | <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> | |
123 | </pre> | |
124 | <p> | |
125 | </p> | |
126 | </div> | |
127 | <div class="section"> | |
128 | <div class="titlepage"><div><div><h3 class="title"> | |
129 | <a name="boost_localfunction.tutorial.Binding"></a><a class="link" href="tutorial.html#boost_localfunction.tutorial.Binding" title="Binding Variables">Binding Variables</a> | |
130 | </h3></div></div></div> | |
131 | <p> | |
132 | Variables in scope (local variables, enclosing function parameters, data | |
133 | members, etc) can be bound to a local function declaration. Only bound variables, | |
134 | static variables, global variables, functions, and enumerations from the | |
135 | enclosing scope are accessible from within the local function body. The types | |
136 | 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> | |
137 | </p> | |
138 | <p> | |
139 | This library introduces the new "keyword" <code class="computeroutput"><span class="identifier">bind</span></code> | |
140 | <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 | |
141 | of a variable in scope to bind (therefore, <code class="computeroutput"><span class="identifier">bind</span></code> | |
142 | cannot be used as a local function parameter type). A variable can be bound | |
143 | by value: | |
144 | </p> | |
145 | <pre class="programlisting"><span class="identifier">bind</span> <span class="emphasis"><em>variable-name</em></span> <span class="comment">// Bind by value.</span> | |
146 | </pre> | |
147 | <p> | |
148 | Or by reference prefixing the variable name with <code class="computeroutput"><span class="special">&</span></code>: | |
149 | </p> | |
150 | <pre class="programlisting"><span class="identifier">bind</span><span class="special">&</span> <span class="emphasis"><em>variable-name</em></span> <span class="comment">// Bind by reference.</span> | |
151 | </pre> | |
152 | <p> | |
153 | Furthermore, the "keyword" <code class="computeroutput"><span class="identifier">bind</span></code> | |
154 | can be prefixed by <code class="computeroutput"><span class="keyword">const</span></code> to | |
155 | bind the variable by constant value: | |
156 | </p> | |
157 | <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> | |
158 | </pre> | |
159 | <p> | |
160 | Or by constant reference: | |
161 | </p> | |
162 | <pre class="programlisting"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&</span> <span class="emphasis"><em>variable-name</em></span> <span class="comment">// Bind by constant value.</span> | |
163 | </pre> | |
164 | <p> | |
165 | Note that when <code class="computeroutput"><span class="keyword">const</span></code> is used, | |
166 | it must always precede <code class="computeroutput"><span class="identifier">bind</span></code>. | |
167 | <a href="#ftn.boost_localfunction.tutorial.Binding.f2" class="footnote" name="boost_localfunction.tutorial.Binding.f2"><sup class="footnote">[10]</sup></a> | |
168 | </p> | |
169 | <p> | |
170 | If a variable is bound by value, then a copy of the variable value is taken | |
171 | at the point of the local function declaration. If a variable is bound by | |
172 | reference instead, the variable will refer to the value it has at the point | |
173 | of the local function call. Furthermore, it is the programmers' responsibility | |
174 | to ensure that variables bound by reference survive the existence scope of | |
175 | the local function otherwise the bound references will be invalid when the | |
176 | local function is called resulting in undefined behaviour (in other words, | |
177 | the usual care in using C++ references must be taken for variables bound | |
178 | by reference). | |
179 | </p> | |
180 | <p> | |
181 | The type of a bound variable is automatically deduced using <a href="http://www.boost.org/libs/typeof" target="_top">Boost.Typeof</a> | |
182 | and it is the exact same type used to declare such a variable in the enclosing | |
183 | scope with the following notes: | |
184 | </p> | |
185 | <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> | |
186 | <li class="listitem"> | |
187 | If a bound variable was declared constant in the enclosing scope, it | |
188 | will always be bound by constant value or constant reference even if | |
189 | <code class="computeroutput"><span class="identifier">bind</span><span class="special">...</span></code> | |
190 | is used instead of <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">...</span></code> | |
191 | . However, if a bound variable was not declared constant in the enclosing | |
192 | scope then it will not be bound as constant unless constant binding is | |
193 | 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 | |
194 | is not supported by <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions" target="_top">C++11 | |
195 | 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> | |
196 | </li> | |
197 | <li class="listitem"> | |
198 | If a bound variable was declared as a reference in the enclosing scope, | |
199 | it will still be bound by value unless it is explicitly bound by reference | |
200 | using <code class="computeroutput"><span class="identifier">bind</span><span class="special">&</span></code> | |
201 | or <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&</span></code>. <a href="#ftn.boost_localfunction.tutorial.Binding.f4" class="footnote" name="boost_localfunction.tutorial.Binding.f4"><sup class="footnote">[12]</sup></a> | |
202 | </li> | |
203 | </ul></div> | |
204 | <p> | |
205 | 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 | |
206 | provide a copy constructor). As with passing parameters to usual C++ functions, | |
207 | programmers might want to bind variables of complex types by (possibly constant) | |
208 | reference instead of by value to avoid expensive copy operations when these | |
209 | variables are bound to a local function. | |
210 | </p> | |
211 | <p> | |
212 | For example, let's program the local function <code class="computeroutput"><span class="identifier">add</span></code> | |
213 | from the example in the <a class="link" href="../index.html#boost_localfunction.introduction" title="Introduction">Introduction</a> | |
214 | section. We bind the local variable <code class="computeroutput"><span class="identifier">factor</span></code> | |
215 | by constant value (because its value should not be modified by the local | |
216 | function), the local variable <code class="computeroutput"><span class="identifier">sum</span></code> | |
217 | by non-constant reference (because its value needs to be updated with the | |
218 | summation result), and program the body to perform the summation (see also | |
219 | <a href="../../../test/add.cpp" target="_top"><code class="literal">add.cpp</code></a>): | |
220 | </p> | |
221 | <p> | |
222 | </p> | |
223 | <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> | |
224 | <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> | |
225 | ||
226 | <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">&</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> | |
227 | <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> | |
228 | <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> | |
229 | ||
230 | <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> | |
231 | <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> | |
232 | <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> | |
233 | ||
234 | <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> | |
235 | <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">report_errors</span><span class="special">();</span> | |
236 | <span class="special">}</span> | |
237 | </pre> | |
238 | <p> | |
239 | </p> | |
240 | </div> | |
241 | <div class="section"> | |
242 | <div class="titlepage"><div><div><h3 class="title"> | |
243 | <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 | |
244 | the Object <code class="computeroutput"><span class="keyword">this</span></code></a> | |
245 | </h3></div></div></div> | |
246 | <p> | |
247 | It is also possible to bind the object <code class="computeroutput"><span class="keyword">this</span></code> | |
248 | when it is in scope (e.g., from an enclosing non-static member function). | |
249 | This is done by using the special symbol <code class="computeroutput"><span class="identifier">this_</span></code> | |
250 | (instead of <code class="computeroutput"><span class="keyword">this</span></code>) as the name | |
251 | of the variable to bind in the local function declaration and also to access | |
252 | 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> | |
253 | </p> | |
254 | <div class="warning"><table border="0" summary="Warning"> | |
255 | <tr> | |
256 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../doc/src/images/warning.png"></td> | |
257 | <th align="left">Warning</th> | |
258 | </tr> | |
259 | <tr><td align="left" valign="top"><p> | |
260 | The library will generate a compile-time error if <code class="computeroutput"><span class="keyword">this</span></code> | |
261 | is mistakenly used instead of <code class="computeroutput"><span class="identifier">this_</span></code> | |
262 | to bind the object in the local function declaration. However, mistakenly | |
263 | 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 | |
264 | local function body will leads to undefined behaviour and it will not necessarily | |
265 | 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. | |
266 | </p></td></tr> | |
267 | </table></div> | |
268 | <p> | |
269 | The object <code class="computeroutput"><span class="keyword">this</span></code> can be bound | |
270 | by value: | |
271 | </p> | |
272 | <pre class="programlisting"><span class="identifier">bind</span> <span class="identifier">this_</span> <span class="comment">// Bind the object `this` by value.</span> | |
273 | </pre> | |
274 | <p> | |
275 | In this case the local function will be able to modify the object when the | |
276 | enclosing scope is not a constant member and it will not be able to modify | |
277 | the object when the enclosing scope is a constant member. Otherwise, the | |
278 | object <code class="computeroutput"><span class="keyword">this</span></code> can be bound by | |
279 | constant value: | |
280 | </p> | |
281 | <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> | |
282 | </pre> | |
283 | <p> | |
284 | In this case the local function will never be able to modify the object (regardless | |
285 | of whether the enclosing scope is a constant member or not). | |
286 | </p> | |
287 | <p> | |
288 | Note that the object <code class="computeroutput"><span class="keyword">this</span></code> can | |
289 | never be bound by reference because C++ does not allow to obtain a reference | |
290 | to <code class="computeroutput"><span class="keyword">this</span></code> (the library will generate | |
291 | a compile-time error if programmers try to use <code class="computeroutput"><span class="identifier">bind</span><span class="special">&</span> <span class="identifier">this_</span></code> | |
292 | or <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">&</span> <span class="identifier">this_</span></code>). | |
293 | Note that <code class="computeroutput"><span class="keyword">this</span></code> is a pointer | |
294 | so the pointed object is never copied even if <code class="computeroutput"><span class="keyword">this</span></code> | |
295 | 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 | |
296 | <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> | |
297 | is an expression and not a variable name). | |
298 | </p> | |
299 | <p> | |
300 | For example, let's program a local function <code class="computeroutput"><span class="identifier">add</span></code> | |
301 | similar to the one in the example from the <a class="link" href="../index.html#boost_localfunction.introduction" title="Introduction">Introduction</a> | |
302 | section but using a member function to illustrate how to bind the object | |
303 | <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>): | |
304 | </p> | |
305 | <p> | |
306 | </p> | |
307 | <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">adder</span> <span class="special">{</span> | |
308 | <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> | |
309 | ||
310 | <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"><</span><span class="keyword">int</span><span class="special">>&</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> | |
311 | ||
312 | <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> | |
313 | <span class="identifier">this_</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="comment">// Use `this_` instead of `this`.</span> | |
314 | <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> | |
315 | ||
316 | <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> | |
317 | <span class="keyword">return</span> <span class="identifier">sum_</span><span class="special">;</span> | |
318 | <span class="special">}</span> | |
319 | ||
320 | <span class="keyword">private</span><span class="special">:</span> | |
321 | <span class="keyword">int</span> <span class="identifier">sum_</span><span class="special">;</span> | |
322 | <span class="special">};</span> | |
323 | </pre> | |
324 | <p> | |
325 | </p> | |
326 | <p> | |
327 | Note that the local function has access to all class members via the bound | |
328 | object <code class="computeroutput"><span class="identifier">this_</span></code> regardless of | |
329 | their access level (<code class="computeroutput"><span class="keyword">public</span></code>, | |
330 | <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 | |
331 | the private data member <code class="computeroutput"><span class="identifier">sum_</span></code>. | |
332 | </p> | |
333 | </div> | |
334 | <div class="section"> | |
335 | <div class="titlepage"><div><div><h3 class="title"> | |
336 | <a name="boost_localfunction.tutorial.templates"></a><a class="link" href="tutorial.html#boost_localfunction.tutorial.templates" title="Templates">Templates</a> | |
337 | </h3></div></div></div> | |
338 | <p> | |
339 | When local functions are programmed within templates, they need to be declared | |
340 | using the special macros <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_TPL.html" title="Macro BOOST_LOCAL_FUNCTION_TPL">BOOST_LOCAL_FUNCTION_TPL</a></code> | |
341 | and <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_NAME_TPL.html" title="Macro BOOST_LOCAL_FUNCTION_NAME_TPL">BOOST_LOCAL_FUNCTION_NAME_TPL</a></code>: | |
342 | <a href="#ftn.boost_localfunction.tutorial.templates.f0" class="footnote" name="boost_localfunction.tutorial.templates.f0"><sup class="footnote">[16]</sup></a> | |
343 | </p> | |
344 | <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">></span> <span class="comment">// This library header.</span> | |
345 | ||
346 | <span class="special">...</span> | |
347 | <span class="special">{</span> <span class="comment">// Some declarative context within a template.</span> | |
348 | <span class="special">...</span> | |
349 | <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> | |
350 | <span class="emphasis"><em>body-code</em></span> | |
351 | <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> | |
352 | <span class="special">...</span> | |
353 | <span class="special">}</span> | |
354 | </pre> | |
355 | <p> | |
356 | The <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_TPL.html" title="Macro BOOST_LOCAL_FUNCTION_TPL">BOOST_LOCAL_FUNCTION_TPL</a></code> | |
357 | and <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_NAME_TPL.html" title="Macro BOOST_LOCAL_FUNCTION_NAME_TPL">BOOST_LOCAL_FUNCTION_NAME_TPL</a></code> | |
358 | macros have the exact same syntax of the <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION.html" title="Macro BOOST_LOCAL_FUNCTION">BOOST_LOCAL_FUNCTION</a></code> | |
359 | and <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_NAME.html" title="Macro BOOST_LOCAL_FUNCTION_NAME">BOOST_LOCAL_FUNCTION_NAME</a></code> | |
360 | macros that we have seen so far. | |
361 | </p> | |
362 | <p> | |
363 | For example, let's program a local function similar to the one from the | |
364 | <a class="link" href="../index.html#boost_localfunction.introduction" title="Introduction">Introduction</a> section | |
365 | but within a template (see also <a href="../../../test/add_template.cpp" target="_top"><code class="literal">add_template.cpp</code></a>): | |
366 | </p> | |
367 | <p> | |
368 | </p> | |
369 | <pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">></span> | |
370 | <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">&</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&</span> <span class="identifier">y</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">T</span><span class="special">&</span> <span class="identifier">z</span><span class="special">)</span> <span class="special">{</span> | |
371 | <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> | |
372 | ||
373 | <span class="comment">// Must use the `..._TPL` macros within templates.</span> | |
374 | <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">&</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> | |
375 | <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> | |
376 | <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> | |
377 | ||
378 | <span class="identifier">add</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span> | |
379 | <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> | |
380 | <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> | |
381 | ||
382 | <span class="keyword">return</span> <span class="identifier">sum</span><span class="special">;</span> | |
383 | <span class="special">}</span> | |
384 | </pre> | |
385 | <p> | |
386 | </p> | |
387 | </div> | |
388 | <div class="footnotes"> | |
389 | <br><hr style="width:100; text-align:left;margin-left: 0"> | |
390 | <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> | |
391 | <span class="bold"><strong>Rationale.</strong></span> The local function name must | |
392 | be passed to the macro <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_NAME.html" title="Macro BOOST_LOCAL_FUNCTION_NAME">BOOST_LOCAL_FUNCTION_NAME</a></code> | |
393 | ending the function definition so this macro can declare a local variable | |
394 | with the local function name to hold the local function object. Therefore | |
395 | the local function name cannot be specified within the <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION.html" title="Macro BOOST_LOCAL_FUNCTION">BOOST_LOCAL_FUNCTION</a></code> | |
396 | and it must appear instead after the local function body (even if that | |
397 | differs from the usual C++ function declaration syntax). | |
398 | </p></div> | |
399 | <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> | |
400 | <span class="bold"><strong>Rationale.</strong></span> If the local function body | |
401 | were instead passed as a macro parameter, it would be expanded on a single | |
402 | line of code (because macros always expand as a single line of code). Therefore, | |
403 | eventual compiler error line numbers would all report the same value and | |
404 | would no longer be useful to pinpoint errors. | |
405 | </p></div> | |
406 | <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> | |
407 | <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> | |
408 | standard does not allow to pass empty parameters to a macro so the macro | |
409 | 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> | |
410 | compilers with properly implemented empty macro parameter support, it would | |
411 | 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 | |
412 | MSVC so this syntax is never allowed to ensure better portability. | |
413 | </p></div> | |
414 | <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> | |
415 | <span class="bold"><strong>Rationale.</strong></span> By binding a variable in scope, | |
416 | the local function declaration is specifying that such a variable should | |
417 | be accessible within the local function body regardless of its type. Semantically, | |
418 | this binding should be seen as an "extension" of the scope of | |
419 | the bound variable from the enclosing scope to the scope of the local function | |
420 | body. Therefore, contrary to the semantic of passing a function parameter, | |
421 | the semantic of binding a variable does not depend on the variable type | |
422 | but just on the variable name: "The variable in scope named <span class="emphasis"><em>x</em></span> | |
423 | should be accessible within the local function named <span class="emphasis"><em>f</em></span>". | |
424 | For example, this reduces maintenance because if a bound variable type | |
425 | is changed, the local function declaration does not have to change. | |
426 | </p></div> | |
427 | <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> | |
428 | Obviously, the token <code class="computeroutput"><span class="identifier">bind</span></code> | |
429 | 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 | |
430 | meta-programming (see the <a class="link" href="implementation.html" title="Annex: Implementation">Implementation</a> | |
431 | section). Therefore, <code class="computeroutput"><span class="identifier">bind</span></code> | |
432 | can be considered a new "keyword" only at the preprocessor meta-programming | |
433 | level within the syntax defined by the macros of this library (thus it | |
434 | is referred to as a "keyword" only within quotes). | |
435 | </p></div> | |
436 | <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> | |
437 | <span class="bold"><strong>Rationale.</strong></span> The library macros could have | |
438 | been implemented to accept both syntaxes <code class="computeroutput"><span class="keyword">const</span> | |
439 | <span class="identifier">bind</span> <span class="special">...</span></code> | |
440 | and <code class="computeroutput"><span class="identifier">bind</span> <span class="keyword">const</span> | |
441 | <span class="special">...</span></code> equivalently. However, handling | |
442 | both syntaxes would have complicated the macro implementation without adding | |
443 | any feature so only one syntax <code class="computeroutput"><span class="keyword">const</span> | |
444 | <span class="identifier">bind</span> <span class="special">...</span></code> | |
445 | is supported. | |
446 | </p></div> | |
447 | <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> | |
448 | An historical note: Constant binding of variables in scope was the | |
449 | main use case that originally motivated the authors in developing this | |
450 | library. The authors needed to locally create a chuck of code to assert | |
451 | some correctness conditions while these assertions were not supposed | |
452 | to modify any of the variables they were using (see the <a href="http://sourceforge.net/projects/contractpp" target="_top">Contract++</a> | |
453 | 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">&</span></code> the variables needed by the assertions | |
454 | and then by programming the local function body to check the assertions. | |
455 | This way if any of the assertions mistakenly changes a bound variable | |
456 | (for example confusing the operator <code class="computeroutput"><span class="special">==</span></code> | |
457 | with <code class="computeroutput"><span class="special">=</span></code>), the compiler | |
458 | correctly generates an error because the bound variable is of <code class="computeroutput"><span class="keyword">const</span></code> type within the local function | |
459 | body (see also <span class="emphasis"><em>constant blocks</em></span> in the <a class="link" href="examples.html" title="Examples">Examples</a> | |
460 | section). | |
461 | </p></div> | |
462 | <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> | |
463 | <span class="bold"><strong>Rationale.</strong></span> Variables originally declared | |
464 | 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">&</span></code> | |
465 | 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> | |
466 | 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">&</span></code> (this is the same binding semantic | |
467 | adopted by <a href="http://www.boost.org/libs/scope_exit" target="_top">Boost.ScopeExit</a>). | |
468 | However, variables originally declared as constants should never loose | |
469 | their <code class="computeroutput"><span class="keyword">const</span></code> qualifier | |
470 | (to prevent their modification not just in the enclosing scope but | |
471 | also in the local scope) thus they are always bound by constant even | |
472 | if <code class="computeroutput"><span class="identifier">bind</span><span class="special">[&]</span></code> | |
473 | is used instead of <code class="computeroutput"><span class="keyword">const</span> <span class="identifier">bind</span><span class="special">[&]</span></code>. | |
474 | </p></div> | |
475 | <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> | |
476 | <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 | |
477 | practise</a> to postfix with an underscore identifiers that are named | |
478 | after keywords (the C++ keyword <code class="computeroutput"><span class="keyword">this</span></code> | |
479 | in this case). The special symbol <code class="computeroutput"><span class="identifier">this_</span></code> | |
480 | is needed because <code class="computeroutput"><span class="keyword">this</span></code> is | |
481 | a reserved C++ keyword so it cannot be used as the name of the internal | |
482 | parameter that passes the bound object to the local function body. It would | |
483 | have been possible to use <code class="computeroutput"><span class="keyword">this</span></code> | |
484 | (instead of <code class="computeroutput"><span class="identifier">this_</span></code>) within | |
485 | the local function body either at the expenses of copying the bound object | |
486 | (which would introduce run-time overhead and also the stringent requirement | |
487 | that the bound object must have a deep copy constructor) or by relying | |
488 | on an <a href="http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/d3a86f27277f713b" target="_top">undefined | |
489 | behaviour of <code class="computeroutput"><span class="keyword">static_cast</span></code></a> | |
490 | (which might not work on all platforms at the cost of portability). | |
491 | </p></div> | |
492 | <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> | |
493 | <span class="bold"><strong>Rationale.</strong></span> The local function body cannot | |
494 | be a static member function of the local functor object in order to support | |
495 | recursion (because the local function name is specified by the <code class="computeroutput"><a class="link" href="../BOOST_LOCAL_FUNCTION_NAME.html" title="Macro BOOST_LOCAL_FUNCTION_NAME">BOOST_LOCAL_FUNCTION_NAME</a></code> | |
496 | macro only after the body so it must be made available via a functor | |
497 | data member named after the local function and local classes cannot have | |
498 | static data members in C++) and nesting (because the argument binding | |
499 | variable must be declared as a data member so it is visible in a local | |
500 | function nested within the body member function) -- see the <a class="link" href="implementation.html" title="Annex: Implementation">Implementation</a> | |
501 | section. Therefore, from within the local function body the variable | |
502 | <code class="computeroutput"><span class="keyword">this</span></code> is visible but it refers | |
503 | to the local functor and not to the bound object. | |
504 | </p></div> | |
505 | <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> | |
506 | <span class="bold"><strong>Rationale.</strong></span> This is possible because of | |
507 | the fix to C++ <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#45" target="_top">defect | |
508 | 45</a> that made inner and local types able to access all outer class | |
509 | members regardless of their access level. | |
510 | </p></div> | |
511 | <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> | |
512 | <span class="bold"><strong>Rationale.</strong></span> Within templates, this library | |
513 | needs to use <code class="computeroutput"><span class="keyword">typename</span></code> to explicitly | |
514 | 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> | |
515 | does not allow to use <code class="computeroutput"><span class="keyword">typename</span></code> | |
516 | outside templates, the special <code class="computeroutput"><span class="special">...</span><span class="identifier">_TPL</span></code> macros are used to indicate that | |
517 | 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. | |
518 | <a href="http://www.open-std.org/JTC1/SC22/WG21/" target="_top">C++11</a> and | |
519 | other compilers might compile local functions within templates even when | |
520 | the <code class="computeroutput"><span class="special">...</span><span class="identifier">_TPL</span></code> | |
521 | 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> | |
522 | macros within templates to maximize portability. | |
523 | </p></div> | |
524 | </div> | |
525 | </div> | |
526 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
527 | <td align="left"></td> | |
528 | <td align="right"><div class="copyright-footer">Copyright © 2009-2012 Lorenzo | |
529 | Caminiti<p> | |
530 | Distributed under the Boost Software License, Version 1.0 (see accompanying | |
531 | 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>) | |
532 | </p> | |
533 | </div></td> | |
534 | </tr></table> | |
535 | <hr> | |
536 | <div class="spirit-nav"> | |
537 | <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> | |
538 | </div> | |
539 | </body> | |
540 | </html> |