tutorial.html 51 KB

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