advanced_topics.qbk 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. [/ Copyright (C) 2009-2012 Lorenzo Caminiti ]
  2. [/ Distributed under the Boost Software License, Version 1.0 ]
  3. [/ (see accompanying file LICENSE_1_0.txt or a copy at ]
  4. [/ http://www.boost.org/LICENSE_1_0.txt) ]
  5. [/ Home at http://www.boost.org/libs/local_function ]
  6. [section Advanced Topics]
  7. This section illustrates advanced usage of this library.
  8. At the bottom there is also a list of known limitations of this library.
  9. [section Default Parameters]
  10. This library allows to specify default values for the local function parameters.
  11. However, the usual C++ syntax for default parameters that uses the assignment symbol `=` cannot be used.
  12. [footnote
  13. *Rationale.*
  14. The assignment symbol `=` cannot be used to specify default parameter values because default values are not part of the parameter type so they cannot be handled using template meta-programming.
  15. Default parameter values need to be separated from the rest of the parameter declaration using the preprocessor.
  16. Specifically, this library needs to use preprocessor meta-programming to remove default values when constructing the local function type and also to count the number of default values to provide the correct set of call operators for the local functor.
  17. Therefore, the symbol `=` cannot be used because it cannot be handled by preprocessor meta-programming (non-alphanumeric symbols cannot be detected by preprocessor meta-programming because they cannot be concatenated by the preprocessor).
  18. ]
  19. The keyword `default` is used instead:
  20. ``[^/parameter-type parameter-name/]``, default`` [^/parameter-default-value/]``, ...
  21. For example, let's program a local function `add(x, y)` where the second parameter `y` is optional and has a default value of `2` (see also [@../../test/add_default.cpp =add_default.cpp=]):
  22. [add_default]
  23. Programmers can define a `WITH_DEFAULT` macro similar to the following if they think it improves readability over the above syntax (see also [@../../test/add_with_default.cpp =add_with_default.cpp=]):
  24. [footnote
  25. The authors do not personally find the use of the `WITH_DEFAULT` macro more readable and they prefer to use the `default` keyword directly.
  26. Furthermore, `WITH_DEFAULT` needs to be defined differently for compilers without variadic macros `#define WITH_DEFAULT (default)` so it can only be defined by programmers based on the syntax they decide to use (see the __No_Variadic_Macros__ section).
  27. ]
  28. [add_with_default_macro]
  29. [add_with_default]
  30. [endsect]
  31. [section Commas and Symbols in Macros]
  32. The C++ preprocessor does not allow commas `,` within macro parameters unless they are wrapped by round parenthesis `()` (see the __Boost_Utility_IdentityType__ documentation for details).
  33. Therefore, using commas within local function parameters and bindings will generate (cryptic) preprocessor errors unless they are wrapped with an extra set of round parenthesis `()` as explained here.
  34. [note
  35. Also macro parameters with commas wrapped by angular parenthesis `<>` (templates, etc) or square parenthesis `[]` (multidimensional array access, etc) need to be wrapped by the extra round parenthesis `()` as explained here (this is because the preprocessor only recognizes the round parenthesis and it does not recognize angular, square, or any other type of parenthesis).
  36. However, macro parameters with commas which are already wrapped by round parenthesis `()` are fine (function calls, some value expressions, etc).
  37. ]
  38. In addition, local function parameter types cannot start with non-alphanumeric symbols (alphanumeric symbols are `A-Z`, `a-z`, and `0-9`).
  39. [footnote
  40. *Rationale.*
  41. This limitation is because this library uses preprocessor token concatenation [^##] to inspect the macro parameters (to distinguish between function parameters, bound variables, etc) and the C++ preprocessor does not allow to concatenate non-alphanumeric tokens.
  42. ]
  43. The library will generate (cryptic) preprocessor errors if a parameter type starts with a non-alphanumeric symbol.
  44. Let's consider the following example:
  45. void BOOST_LOCAL_FUNCTION(
  46. const std::map<std::string, size_t>& m, // (1) Error.
  47. ::sign_t sign, // (2) Error.
  48. const size_t& factor,
  49. default key_sizeof<std::string, size_t>::value, // (3) Error.
  50. const std::string& separator, default cat(":", " ") // (4) OK.
  51. ) {
  52. ...
  53. } BOOST_LOCAL_FUNCTION_NAME(f)
  54. [*(1)] The parameter type `const std::map<std::string, size_t>&` contains a comma `,` after the first template parameter `std::string`.
  55. This comma is not wrapped by any round parenthesis `()` thus it will cause a preprocessor error.
  56. [footnote
  57. The preprocessor always interprets unwrapped commas as separating macro parameters.
  58. Thus in this case the comma will indicate to the preprocessor that the first macro parameter is `const std::map<std::tring`, the second macro parameter is `size_t>& m`, etc instead of passing `const std::map<std::string, size_t>& m` as a single macro parameter.
  59. ]
  60. The __Boost_Utility_IdentityType__ macro `BOOST_IDENTITY_TYPE((`[^['type-with-commas]]`))` defined in the =boost/utility/identity_type.hpp= header can be used to wrap a type within extra parenthesis `()` so to overcome this problem:
  61. #include <boost/utility/identity_type.hpp>
  62. void BOOST_LOCAL_FUNCTION(
  63. BOOST_IDENTITY_TYPE((const std::map<std::string, size_t>&)) m, // OK.
  64. ...
  65. ) {
  66. ...
  67. } BOOST_LOCAL_FUNCTION_NAME(f)
  68. This macro expands to an expression that evaluates (at compile-time) exactly to the specified type (furthermore, this macro does not use variadic macros so it works on any __CXX03__ compiler).
  69. Note that a total of two set of parenthesis `()` are needed: The parenthesis to invoke the `BOOST_IDENTITY_TYPE(...)` macro plus the parenthesis to wrap the type expression (and therefore any comma `,` that it contains) passed as parameter to the `BOOST_IDENTITY_TYPE((...))` macro.
  70. Finally, the `BOOST_IDENTITY_TYPE` macro must be prefixed by the `typename` keyword `typename BOOST_IDENTITY_TYPE(`[^['parenthesized-type]]`)` when used together with the [macroref BOOST_LOCAL_FUNCTION_TPL] macro within templates.
  71. [note
  72. Often, there might be better ways to overcome this limitation that lead to code which is more readable than the one using the `BOOST_IDENTITY_TYPE` macro.
  73. ]
  74. For example, in this case a `typedef` from the enclosing scope could have been used to obtain the following valid and perhaps more readable code:
  75. typedef std::map<std::string, size_t> map_type;
  76. void BOOST_LOCAL_FUNCTION(
  77. const map_type& m, // OK (and more readable).
  78. ...
  79. ) BOOST_LOCAL_FUNCTION_NAME(f)
  80. [*(2)] The parameter type `::sign_t` starts with the non-alphanumeric symbols `::` thus it will generate preprocessor errors if used as a local function parameter type.
  81. The `BOOST_IDENTITY_TYPE` macro can also be used to overcome this issue:
  82. void BOOST_LOCAL_FUNCTION(
  83. ...
  84. BOOST_IDENTITY_TYPE((::sign_t)) sign, // OK.
  85. ...
  86. ) {
  87. ...
  88. } BOOST_LOCAL_FUNCTION_NAME(f)
  89. [note
  90. Often, there might be better ways to overcome this limitation that lead to code which is more readable than the one using the `BOOST_IDENTITY_TYPE` macro.
  91. ]
  92. For example, in this case the symbols `::` could have been simply dropped to obtain the following valid and perhaps more readable code:
  93. void BOOST_LOCAL_FUNCTION(
  94. ...
  95. sign_t sign, // OK (and more readable).
  96. ...
  97. ) {
  98. ...
  99. } BOOST_LOCAL_FUNCTION_NAME(f)
  100. [*(3)] The default parameter value `key_sizeof<std::string, size_t>::value` contains a comma `,` after the first template parameter `std::string`.
  101. Again, this comma is not wrapped by any parenthesis `()` so it will cause a preprocessor error.
  102. Because this is a value expression (and not a type expression), it can simply be wrapped within an extra set of round parenthesis `()`:
  103. void BOOST_LOCAL_FUNCTION(
  104. ...
  105. const size_t& factor,
  106. default (key_sizeof<std::string, size_t>::value), // OK.
  107. ...
  108. ) {
  109. ...
  110. } BOOST_LOCAL_FUNCTION_NAME(f)
  111. [*(4)] The default parameter value `cat(":", " ")` is instead fine because it contains a comma `,` which is already wrapped by the parenthesis `()` of the function call `cat(...)`.
  112. Consider the following complete example (see also [@../../test/macro_commas.cpp =macro_commas.cpp=]):
  113. [macro_commas]
  114. [endsect]
  115. [section Assignments and Returns]
  116. Local functions are function objects so it is possible to assign them to other functors like __Boost_Function__'s `boost::function` in order to store the local function into a variable, pass it as a parameter to another function, or return it from the enclosing function.
  117. For example (see also [@../../test/return_assign.cpp =return_assign.cpp=]):
  118. [return_assign]
  119. [warning
  120. As with __CXX11_lambda_functions__, programmers are responsible to ensure that bound variables are valid in any scope where the local function object is called.
  121. Returning and calling a local function outside its declaration scope will lead to undefined behaviour if any of the bound variable is no longer valid in the scope where the local function is called (see the __Examples__ section for more examples on the extra care needed when returning a local function as a closure).
  122. It is always safe instead to call a local function within its enclosing scope.
  123. ]
  124. In addition, a local function can bind and call other local functions.
  125. Local functions should always be bound by constant reference `const bind&` to avoid unnecessary copies.
  126. For example, the following local function `inc_sum` binds the local function `inc` so `inc_sum` can call `inc` (see aslo [@../../test/transform.cpp =transform.cpp=]):
  127. [transform]
  128. [endsect]
  129. [section Nesting]
  130. It is possible to nest local functions into one another.
  131. For example (see also [@../../test/nesting.cpp =nesting.cpp=]):
  132. [nesting]
  133. [endsect]
  134. [section Accessing Types (concepts, etc)]
  135. This library never requires to explicitly specify the type of bound variables (e.g., this reduces maintenance because the local function declaration and definition do not have to change even if the bound variable types change as long as the semantics of the local function remain valid).
  136. From within local functions, programmers can access the type of a bound variable using the following macro:
  137. BOOST_LOCAL_FUNCTION_TYPEOF(``/bound-variable-name/``)
  138. The [macroref BOOST_LOCAL_FUNCTION_TYPEOF] macro expands to a type expression that evaluates (at compile-time) to the fully qualified type of the bound variable with the specified name.
  139. This type expression is fully qualified in the sense that it will be constant if the variable is bound by constant `const bind[&]` and it will also be a reference if the variable is bound by reference `[const] bind&` (if needed, programmers can remove the `const` and `&` qualifiers using `boost::remove_const` and `boost::remove_reference`, see __Boost_TypeTraits__).
  140. The deduced bound type can be used within the body to check concepts, declare local variables, etc.
  141. For example (see also [@../../test/typeof.cpp =typeof.cpp=] and [@../../test/addable.hpp =addable.hpp=]):
  142. [typeof]
  143. Within templates, [macroref BOOST_LOCAL_FUNCTION_TYPEOF] should not be prefixed by the `typename` keyword but eventual type manipulations need the `typename` prefix as usual (see also [@../../test/typeof_template.cpp =typeof_template.cpp=] and [@../../test/addable.hpp =addable.hpp=]):
  144. [typeof_template]
  145. In this context, it is best to use the [macroref BOOST_LOCAL_FUNCTION_TYPEOF] macro instead of using __Boost_Typeof__ to reduce the number of times that __Boost_Typeof__ is invoked (either the library already internally used __Boost_Typeof__ once, in which case using this macro will not use __Boost_Typeof__ again, or the bound variable type is explicitly specified by programmers as shown be below, in which case using this macro will not use __Boost_Typeof__ at all).
  146. Furthermore, within the local function body it possible to access the result type using `result_type`, the type of the first parameter using `arg1_type`, the type of the second parameter using `arg2_type`, etc.
  147. [footnote
  148. *Rationale.*
  149. The type names `result_type` and `arg`[^['N]]`_type` follow the __Boost_TypeTraits__ naming conventions for function traits.
  150. ]
  151. [endsect]
  152. [section Specifying Types (no Boost.Typeof)]
  153. While not required, it is possible to explicitly specify the type of bound variables so the library will not internally use __Boost_Typeof__ to automatically deduce the types.
  154. When specified, the bound variable type must follow the `bind` "keyword" and it must be wrapped within round parenthesis `()`:
  155. bind(``/variable-type/``) ``/variable-name/`` // Bind by value with explicit type.
  156. bind(``/variable-type/``)& ``/variable-name/`` // Bind by reference with explicit type.
  157. const bind(``/variable-type/``) ``/variable-name/`` // Bind by constant value with explicit type.
  158. const bind(``/variable-type/``)& ``/variable-name/`` // Bind by constant reference with explicit type.
  159. bind(``/class-type/``*) this_ // Bind object `this` with explicit type.
  160. const bind(``/class-type/``*) this_ // Bind object `this` by constant with explicit type.
  161. Note that within the local function body it is always possible to abstract the access to the type of a bound variable using [macroref BOOST_LOCAL_FUNCTION_TYPEOF] (even when the bound variable type is explicitly specified in the local function declaration).
  162. The library also uses __Boost_Typeof__ to determine the local function result type (because this type is specified outside the [macroref BOOST_LOCAL_FUNCTION] macro).
  163. Thus it is also possible to specify the local function result type as one of the [macroref BOOST_LOCAL_FUNCTION] macro parameters prefixing it by `return` so the library will not use __Boost_Typeof__ to deduce the result type:
  164. BOOST_LOCAL_FUNCTION_TYPE(return ``[^/result-type/]``, ...)
  165. Note that the result type must be specified only once either before the macro (without the `return` prefix) or as one of the macro parameters (with the `return` prefix).
  166. As always, the result type can be `void` to declare a function that returns nothing (so `return void` is allowed when the result type is specified as one of the macro parameters).
  167. The following example specifies all bound variables and result types (see also [@../../test/add_typed.cpp =add_typed.cpp=]):
  168. [footnote
  169. In the examples of this documentation, bound variables, function parameters, and the result type are specified in this order because this is the order used by __CXX11_lambda_functions__.
  170. However, the library accepts bound variables, function parameters, and the result type in any order.
  171. ]
  172. [add_typed]
  173. Unless necessary, it is recommended to not specify the bound variable and result types.
  174. Let the library deduce these types so the local function syntax will be more concise and the local function declaration will not have to change if a bound variable type changes (reducing maintenance).
  175. [note
  176. When all bound variable and result types are explicitly specified, the library implementation will not use __Boost_Typeof__.
  177. ]
  178. [endsect]
  179. [section Inlining]
  180. Local functions can be declared [@http://en.wikipedia.org/wiki/Inline_function inline] to increase the chances that the compiler will be able to reduce the run-time of the local function call by inlining the generated assembly code.
  181. A local function is declared inline by prefixing its name with the keyword `inline`:
  182. ``/result-type/`` BOOST_LOCAL_FUNCTION(``/parameters/``) {
  183. ... // Body.
  184. } BOOST_LOCAL_FUNCTION_NAME(inline ``/name/``) // Inlining.
  185. When inlining a local function, note the following:
  186. * On __CXX03__ compliant compilers, inline local functions always have a run-time comparable to their equivalent implementation that uses local functors (see the __Alternatives__ section).
  187. However, inline local functions have the important limitation that they cannot be assigned to other functors (like `boost::function`) and they cannot be passed as template parameters.
  188. * On __CXX11__ compilers, `inline` has no effect because this library will automatically generate code that uses __CXX11__ specific features to inline the local function calls whenever possible even if the local function is not declared inline.
  189. Furthermore, non __CXX11__ local functions can always be passes as template parameters even when they are declared inline.
  190. [footnote
  191. *Rationale.*
  192. This library uses an indirect function call via a function pointer in order to pass the local function as a template parameter (see the __Implementation__ section).
  193. No compiler has yet been observed to be able to inline function calls when they use such indirect function pointer calls.
  194. Therefore, inline local functions do not use such indirect function pointer call (so they are more likely to be optimized) but because of that they cannot be passed as template parameters.
  195. The indirect function pointer call is needed on __CXX03__ but it is not needed on __CXX11__ (see __N2657__ and __Boost_Config__'s `BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS`) thus this library automatically generates local function calls that can be inline on __CXX11__ compilers (even when the local function is not declared inline).
  196. ]
  197. [important
  198. It is recommended to not declare a local function inline unless it is strictly necessary for optimizing pure __CXX03__ compliant code (because in all other cases this library will automatically take advantage of __CXX11__ features to optimize the local function calls while always allowing to pass the local function as a template parameter).
  199. ]
  200. For example, the following local function is declared inline (thus a for-loop needs to be used for portability instead of passing the local function as a template parameter to the `std::for_each` algorithm, see also [@../../test/add_inline.cpp =add_inline.cpp=]):
  201. [add_inline]
  202. [endsect]
  203. [section Recursion]
  204. Local functions can be declared [@http://en.wikipedia.org/wiki/Recursion_(computer_science)#Recursive_procedures recursive] so a local function can recursively call itself from its body (as usual with C++ functions).
  205. A local function is declared recursive by prefixing its name with the `recursive` "keyword" (thus `recursive` cannot be used as a local function name):
  206. ``/result-type/`` BOOST_LOCAL_FUNCTION(``/parameters/``) {
  207. ... // Body.
  208. } BOOST_LOCAL_FUNCTION_NAME(recursive ``/name/``) // Recursive.
  209. For example, the following local function is used to recursively calculate the factorials of all the numbers in the specified vector (see also [@../../test/factorial.cpp =factorial.cpp=]):
  210. [factorial]
  211. Compilers have not been observed to be able to inline recursive local function calls not even when the recursive local function is also declared inline:
  212. ... BOOST_LOCAL_FUNCTION_NAME(inline recursive factorial)
  213. Recursive local functions should never be called outside their declaration scope.
  214. [footnote
  215. *Rationale.*
  216. This limitation comes from the fact that the global functor used to pass the local function as a template parameter (and eventually returned outside the declarations scope) does not know the local function name so the local function name used for recursive call cannot be set in the global functor.
  217. This limitation together with preventing the possibility for inlining are the reasons why local functions are not recursive unless programmers explicitly declare them `recursive`.
  218. ]
  219. [warning
  220. If a local function is returned from the enclosing function and called in a different scope, the behaviour is undefined (and it will likely result in a run-time error).
  221. ]
  222. This is not a limitation with respect to __CXX11_lambda_functions__ because lambdas can never call themselves recursively (in other words, there is no recursive lambda function that can successfully be called outside its declaration scope because there is no recursive lambda function at all).
  223. [endsect]
  224. [section Overloading]
  225. Because local functions are functors, it is possible to overload them using the `boost::overloaded_function` functor of __Boost_Functional_OverloadedFunction__ from the =boost/functional/overloaded_function.hpp= header (see the __Boost_Functional_OverloadedFunction__ documentation for details).
  226. In the following example, the overloaded function object `add` can be called with signatures from either the local function `add_s`, or the local function `add_d`, or the local function `add_d` with its extra default parameter, or the function pointer `add_i` (see also [@../../test/overload.cpp =overload.cpp=]):
  227. [overload_decl]
  228. [overload]
  229. [endsect]
  230. [section Exception Specifications]
  231. It is possible to program exception specifications for local functions by specifying them after the [macroref BOOST_LOCAL_FUNCTION] macro and before the body code block `{ ... }`.
  232. [important
  233. Note that the exception specifications only apply to the body code specified by programmers and they do not apply to the rest of the code automatically generated by the macro expansions to implement local functions.
  234. For example, even if the body code is specified to throw no exception using `throw () { ... }`, the execution of the library code automatically generated by the macros could still throw (if there is no memory, etc).
  235. ]
  236. For example (see also [@../../test/add_except.cpp =add_except.cpp=]):
  237. [add_except]
  238. [endsect]
  239. [section Storage Classifiers]
  240. Local function parameters support the storage classifiers as usual in __CXX03__.
  241. The `auto` storage classifier is specified as:
  242. [footnote
  243. The `auto` storage classifier is part of the __CXX03__ standard and therefore supported by this library.
  244. However, the meaning and usage of the `auto` keyword changed in __CXX11__.
  245. Therefore, use the `auto` storage classifier with the usual care in order to avoid writing __CXX03__ code that might not work on __CXX11__.
  246. ]
  247. auto ``/parameter-type parameter-name/``
  248. The `register` storage classifier is specified as:
  249. register ``/parameter-type parameter-name/``
  250. For example (see also [@../../test/add_classifiers.cpp =add_classifiers.cpp=]):
  251. [add_classifiers]
  252. [endsect]
  253. [section Same Line Expansions]
  254. In general, it is not possible to expand the [macroref BOOST_LOCAL_FUNCTION], [macroref BOOST_LOCAL_FUNCTION_TPL] macros multiple times on the same line.
  255. [footnote
  256. *Rationale.*
  257. The [macroref BOOST_LOCAL_FUNCTION] and [macroref BOOST_LOCAL_FUNCTION_TPL] macros internally use `__LINE__` to generate unique identifiers.
  258. Therefore, if these macros are expanded more than on time on the same line, the generated identifiers will no longer be unique and the code will not compile.
  259. (This restriction does not apply to MSVC and other compilers that provide the non-standard `__COUNTER__` macro.)
  260. Note that the [macroref BOOST_LOCAL_FUNCTION_NAME] macro can always be expanded multiple times on the same line because the unique local function name (and not `__LINE__`) is used by this macro to generate unique identifiers (so there is no need for a `BOOST_LOCAL_FUNCTION_NAME_ID` macro).
  261. ]
  262. Therefore, this library provides additional macros [macroref BOOST_LOCAL_FUNCTION_ID] and [macroref BOOST_LOCAL_FUNCTION_ID_TPL] which can be expanded multiple times on the same line as long as programmers specify unique identifiers as the macros' first parameters.
  263. The unique identifier can be any token (not just numeric) that can be successfully concatenated by the preprocessor (e.g., `local_function_number_1_at_line_123`).
  264. [footnote
  265. Because there are restrictions on the set of tokens that the preprocessor can concatenate and because not all compilers correctly implement these restrictions, it is in general recommended to specify unique identifiers as a combination of alphanumeric tokens.
  266. ]
  267. The [macroref BOOST_LOCAL_FUNCTION_ID] and [macroref BOOST_LOCAL_FUNCTION_ID_TPL] macros accept local function parameter declaration lists using the exact same syntax as [macroref BOOST_LOCAL_FUNCTION].
  268. For example (see also [@../../test/same_line.cpp =same_line.cpp=]):
  269. [same_line]
  270. As shown by the example above, the [macroref BOOST_LOCAL_FUNCTION_ID] and [macroref BOOST_LOCAL_FUNCTION_ID_TPL] macros are especially useful when it is necessary to invoke them multiple times within a user-defined macro (because the preprocessor expands all nested macros on the same line).
  271. [endsect]
  272. [section Limitations (operators, etc)]
  273. The following table summarizes all C++ function features indicating those features that are not supported by this library for local functions.
  274. [table
  275. [
  276. [ C++ Function Feature ]
  277. [ Local Function Support ]
  278. [ Comment ]
  279. ]
  280. [
  281. [ `export` ]
  282. [ No. ]
  283. [ This is not supported because local functions cannot be templates (plus most C++ compilers do not implement `export` at all). ]
  284. ]
  285. [
  286. [ `template<`[^['template-parameter-list]]`>` ]
  287. [ No. ]
  288. [ This is not supported because local functions are implemented using local classes and __CXX03__ local classes cannot be templates. ]
  289. ]
  290. [
  291. [ `explicit` ]
  292. [ No. ]
  293. [ This is not supported because local functions are not constructors. ]
  294. ]
  295. [
  296. [ `inline` ]
  297. [ Yes. ]
  298. [ Local functions can be specified `inline` to improve the chances that __CXX03__ compilers can optimize the local function call run-time (but `inline` local functions cannot be passed as template parameters on __CXX03__ compilers, see the __Advanced_Topics__ section). ]
  299. ]
  300. [
  301. [ `extern` ]
  302. [ No. ]
  303. [ This is not supported because local functions are always defined locally within the enclosing scope and together with their declarations. ]
  304. ]
  305. [
  306. [ `static` ]
  307. [ No. ]
  308. [ This is not supported because local functions are not member functions. ]
  309. ]
  310. [
  311. [ `virtual` ]
  312. [ No. ]
  313. [ This is not supported because local functions are not member functions.
  314. [footnote
  315. *Rationale.*
  316. It would be possible to make a local function class inherit from another local function class.
  317. However, this "inheritance" feature is not implemented because it seemed of [@http://lists.boost.org/Archives/boost/2010/09/170895.php no use] given that local functions can be bound to one another thus they can simply call each other directly without recurring to dynamic binding or base function calls.
  318. ]
  319. ]
  320. ]
  321. [
  322. [ [^/result-type/] ]
  323. [ Yes. ]
  324. [ This is supported (see the __Tutorial__ section). ]
  325. ]
  326. [
  327. [ [^/function-name/] ]
  328. [ Yes. ]
  329. [ Local functions are named and they can call themselves recursively but they cannot be operators (see the __Tutorial__ and __Advanced_Topics__ sections). ]
  330. ]
  331. [
  332. [ [^/parameter-list/] ]
  333. [ Yes. ]
  334. [ This is supported and it also supports the `auto` and `register` storage classifiers, default parameters, and binding of variables in scope (see the __Tutorial__ and __Advanced_Topics__ sections). ]
  335. ]
  336. [
  337. [ Trailing `const` qualifier ]
  338. [ No. ]
  339. [ This is not supported because local functions are not member functions. ]
  340. ]
  341. [
  342. [ Trailing `volatile` qualifier ]
  343. [ No. ]
  344. [ This is not supported because local functions are not member functions. ]
  345. ]
  346. ]
  347. [heading Operators]
  348. Local functions cannot be operators.
  349. Naming a local function `operator...` will generate a compile-time error.
  350. [footnote
  351. *Rationale.*
  352. This is the because a local function name must be a valid local variable name (the local variable used to hold the local functor) and operators cannot be used as local variable names.
  353. ]
  354. For example, the following code does not compile (see also [@../../test/operator_error.cpp =operator_error.cpp=]):
  355. [operator_error]
  356. [heading Goto]
  357. It is possible to jump with a `goto` within the local function body.
  358. For example, the following compiles (see also [@../../test/goto.cpp =goto.cpp=]):
  359. [goto]
  360. However, it is not possible to jump with a `goto` from within the local function body to to a label defined in the enclosing scope.
  361. For example, the following does not compile (see also [@../../test/goto_error.cpp =goto_error.cpp=]):
  362. [goto_error]
  363. [endsect]
  364. [endsect]