Chris@16
|
1
|
Chris@16
|
2 // Copyright (C) 2009-2012 Lorenzo Caminiti
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0
|
Chris@16
|
4 // (see accompanying file LICENSE_1_0.txt or a copy at
|
Chris@16
|
5 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 // Home at http://www.boost.org/libs/local_function
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_LOCAL_FUNCTION_HPP_
|
Chris@16
|
9 #define BOOST_LOCAL_FUNCTION_HPP_
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef DOXYGEN
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/local_function/aux_/macro/decl.hpp>
|
Chris@16
|
14 #include <boost/local_function/aux_/macro/name.hpp>
|
Chris@16
|
15 #include <boost/local_function/aux_/macro/typeof.hpp>
|
Chris@16
|
16 #include <boost/local_function/aux_/preprocessor/traits/decl.hpp>
|
Chris@16
|
17 #include <boost/local_function/detail/preprocessor/line_counter.hpp>
|
Chris@16
|
18 #include <boost/local_function/detail/preprocessor/void_list.hpp>
|
Chris@16
|
19 #include <boost/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 // PUBLIC //
|
Chris@16
|
22
|
Chris@16
|
23 #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
|
Chris@16
|
24 # define BOOST_LOCAL_FUNCTION_ID(id, declarations) \
|
Chris@16
|
25 BOOST_LOCAL_FUNCTION_AUX_DECL(id, 0 /* not within template */, \
|
Chris@16
|
26 BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS( \
|
Chris@16
|
27 BOOST_LOCAL_FUNCTION_DETAIL_PP_VOID_LIST( \
|
Chris@16
|
28 declarations)))
|
Chris@16
|
29 # define BOOST_LOCAL_FUNCTION(declarations) \
|
Chris@16
|
30 BOOST_LOCAL_FUNCTION_ID( \
|
Chris@16
|
31 BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER, declarations)
|
Chris@16
|
32 # define BOOST_LOCAL_FUNCTION_ID_TPL(id, declarations) \
|
Chris@16
|
33 BOOST_LOCAL_FUNCTION_AUX_DECL(id, 1 /* within template */, \
|
Chris@16
|
34 BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS( \
|
Chris@16
|
35 BOOST_LOCAL_FUNCTION_DETAIL_PP_VOID_LIST( \
|
Chris@16
|
36 declarations)))
|
Chris@16
|
37 # define BOOST_LOCAL_FUNCTION_TPL(declarations) \
|
Chris@16
|
38 BOOST_LOCAL_FUNCTION_ID_TPL( \
|
Chris@16
|
39 BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER, declarations)
|
Chris@16
|
40 #else // VARIADIC
|
Chris@16
|
41 # define BOOST_LOCAL_FUNCTION_ID(id, ...) \
|
Chris@16
|
42 BOOST_LOCAL_FUNCTION_AUX_DECL(id, 0 /* not within template */, \
|
Chris@16
|
43 BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS( \
|
Chris@16
|
44 BOOST_LOCAL_FUNCTION_DETAIL_PP_VOID_LIST(__VA_ARGS__)))
|
Chris@16
|
45 # define BOOST_LOCAL_FUNCTION(...) \
|
Chris@16
|
46 BOOST_LOCAL_FUNCTION_ID( \
|
Chris@16
|
47 BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER, __VA_ARGS__)
|
Chris@16
|
48 # define BOOST_LOCAL_FUNCTION_ID_TPL(id, ...) \
|
Chris@16
|
49 BOOST_LOCAL_FUNCTION_AUX_DECL(id, 1 /* within template */, \
|
Chris@16
|
50 BOOST_LOCAL_FUNCTION_AUX_PP_DECL_TRAITS( \
|
Chris@16
|
51 BOOST_LOCAL_FUNCTION_DETAIL_PP_VOID_LIST(__VA_ARGS__)))
|
Chris@16
|
52 # define BOOST_LOCAL_FUNCTION_TPL(...) \
|
Chris@16
|
53 BOOST_LOCAL_FUNCTION_ID_TPL( \
|
Chris@16
|
54 BOOST_LOCAL_FUNCTION_DETAIL_PP_LINE_COUNTER, __VA_ARGS__)
|
Chris@16
|
55 #endif // VARIADIC
|
Chris@16
|
56
|
Chris@16
|
57 #define BOOST_LOCAL_FUNCTION_NAME(qualified_name) \
|
Chris@16
|
58 BOOST_LOCAL_FUNCTION_AUX_NAME(0 /* not within template */, qualified_name)
|
Chris@16
|
59 #define BOOST_LOCAL_FUNCTION_NAME_TPL(qualified_name) \
|
Chris@16
|
60 BOOST_LOCAL_FUNCTION_AUX_NAME(1 /* within template */, qualified_name)
|
Chris@16
|
61
|
Chris@16
|
62 #define BOOST_LOCAL_FUNCTION_TYPEOF(bound_variable_name) \
|
Chris@16
|
63 BOOST_LOCAL_FUNCTION_AUX_TYPEOF_TYPE(bound_variable_name)
|
Chris@16
|
64
|
Chris@16
|
65 // DOCUMENTATION //
|
Chris@16
|
66
|
Chris@16
|
67 #else // DOXYGEN
|
Chris@16
|
68
|
Chris@16
|
69 /** @file
|
Chris@16
|
70 @brief Local functions allow to program functions locally, within other
|
Chris@16
|
71 functions, and directly within the scope where they are needed.
|
Chris@16
|
72 */
|
Chris@16
|
73
|
Chris@16
|
74 /**
|
Chris@16
|
75 @brief This macro is used to start a local function declaration.
|
Chris@16
|
76
|
Chris@16
|
77 This macro must be used within a declarative context, it must follow the local
|
Chris@16
|
78 function result type, it must be followed by the local function body code, and
|
Chris@16
|
79 then by the @RefMacro{BOOST_LOCAL_FUNCTION_NAME} macro (see the
|
Chris@16
|
80 @RefSect{tutorial, Tutorial} and @RefSect{advanced_topics, Advanced Topics}
|
Chris@16
|
81 sections):
|
Chris@16
|
82 @code
|
Chris@16
|
83 { // Some declarative context.
|
Chris@16
|
84 ...
|
Chris@16
|
85 result_type BOOST_LOCAL_FUNCTION(declarations) {
|
Chris@16
|
86 ... // Body code.
|
Chris@16
|
87 } BOOST_LOCAL_FUNCTION_NAME(qualified_name)
|
Chris@16
|
88 ...
|
Chris@16
|
89 }
|
Chris@16
|
90 @endcode
|
Chris@16
|
91
|
Chris@16
|
92 As usual, exceptions specifications can be optionally programmed just after the
|
Chris@16
|
93 macro and before the body code block <c>{ ... }</c> (but the exception
|
Chris@16
|
94 specifications will only apply to the body code and not to the library code
|
Chris@16
|
95 automatically generated by the macro expansion, see the
|
Chris@16
|
96 @RefSect{advanced_topics, Advanced Topics} section).
|
Chris@16
|
97
|
Chris@16
|
98 Within templates, the special macros @RefMacro{BOOST_LOCAL_FUNCTION_TPL}
|
Chris@16
|
99 and @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL} must be used.
|
Chris@16
|
100
|
Chris@16
|
101 @Params
|
Chris@16
|
102 @Param{declarations,
|
Chris@16
|
103 On compilers that support variadic macros\, the parameter declarations are
|
Chris@16
|
104 defined by the following grammar:
|
Chris@16
|
105 @code
|
Chris@16
|
106 declarations:
|
Chris@16
|
107 void | declaration_tuple | declaration_sequence
|
Chris@16
|
108 declaration_tuple:
|
Chris@16
|
109 declaration\, declaration\, ...
|
Chris@16
|
110 declaration_sequence:
|
Chris@16
|
111 (declaration) (declaration) ...
|
Chris@16
|
112 declaration:
|
Chris@16
|
113 bound_variable | parameter | default_value | result_type
|
Chris@16
|
114 bound_variable:
|
Chris@16
|
115 [const] bind [(variable_type)] [&] variable_name
|
Chris@16
|
116 parameter:
|
Chris@16
|
117 [auto | register] parameter_type parameter_name
|
Chris@16
|
118 default_value:
|
Chris@16
|
119 default parameter_default_value
|
Chris@16
|
120 result_type:
|
Chris@16
|
121 return function_result_type
|
Chris@16
|
122 @endcode
|
Chris@16
|
123 On compilers that do not support variadic macros\, <c>declaration_tuple</c>
|
Chris@16
|
124 cannot be used:
|
Chris@16
|
125 @code
|
Chris@16
|
126 declarations:
|
Chris@16
|
127 void | declaration_sequence
|
Chris@16
|
128 @endcode
|
Chris@16
|
129
|
Chris@16
|
130 (Lexical conventions: <c>token1 | token2</c> means either <c>token1</c> or
|
Chris@16
|
131 <c>token2</c>; <c>[token]</c> means either <c>token</c> or nothing;
|
Chris@16
|
132 <c>{expression}</c> means the token resulting from the expression.)
|
Chris@16
|
133 }
|
Chris@16
|
134 @EndParams
|
Chris@16
|
135
|
Chris@16
|
136 Note that on compilers that support variadic macros, commas can be used to
|
Chris@16
|
137 separate the declarations resembling more closely the usual C++ function
|
Chris@16
|
138 declaration syntax (this is the preferred syntax).
|
Chris@16
|
139 However, for portability, on all C++ compilers (with and without variadic
|
Chris@16
|
140 macros) the same library macros also accept parameter declarations specified as
|
Chris@16
|
141 a Boost.Preprocessor sequence separated by round parenthesis <c>()</c>.
|
Chris@16
|
142
|
Chris@16
|
143 When binding the object <c>this</c>, the special symbol <c>this_</c> needs to
|
Chris@16
|
144 be used instead of <c>this</c> as the name of the variable to bind and also
|
Chris@16
|
145 within the local function body to access the object.
|
Chris@16
|
146 (Mistakenly using <c>this</c> instead of <c>this_</c> might not always result in a compiler error and will in general result in undefined behaviour.)
|
Chris@16
|
147
|
Chris@16
|
148 The result type must either be specified just before the macro or within the
|
Chris@16
|
149 macro declarations prefixed by <c>return</c> (but not in both places).
|
Chris@16
|
150
|
Chris@16
|
151 Within the local function body it possible to access the result type using <c>result_type</c>, the type of the first parameter using <c>arg1_type</c>, the type of the second parameter using <c>arg2_type</c>, etc.
|
Chris@16
|
152 The bound variable types can be accessed using @RefMacro{BOOST_LOCAL_FUNCTION_TYPEOF}.
|
Chris@16
|
153
|
Chris@16
|
154 This macro cannot be portably expanded multiple times on the same line.
|
Chris@16
|
155 In these cases, use the @RefMacro{BOOST_LOCAL_FUNCTION_ID} macro instead.
|
Chris@16
|
156
|
Chris@16
|
157 The maximum number of local function parameters (excluding bound variables) is
|
Chris@16
|
158 specified by the configuration macro
|
Chris@16
|
159 @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_ARITY_MAX}.
|
Chris@16
|
160 The maximum number of bound variables is specified by the configuration macro
|
Chris@16
|
161 @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_BIND_MAX}.
|
Chris@16
|
162 The configuration macro
|
Chris@16
|
163 @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_LOCALS_AS_TPARAMS} can be used to force
|
Chris@16
|
164 optimizations that reduce the local function call run-time overhead.
|
Chris@16
|
165
|
Chris@16
|
166 @Note Local functions are functors so they can be assigned to other functors
|
Chris@16
|
167 like <c>boost::function</c> (see Boost.Function).
|
Chris@16
|
168
|
Chris@16
|
169 @See @RefSect{tutorial, Tutorial} section,
|
Chris@16
|
170 @RefSect{advanced_topics, Advanced Topics} section,
|
Chris@16
|
171 @RefMacro{BOOST_LOCAL_FUNCTION_NAME}, @RefMacro{BOOST_LOCAL_FUNCTION_TPL},
|
Chris@16
|
172 @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL},
|
Chris@16
|
173 @RefMacro{BOOST_LOCAL_FUNCTION_TYPEOF}, @RefMacro{BOOST_LOCAL_FUNCTION_ID},
|
Chris@16
|
174 @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_ARITY_MAX},
|
Chris@16
|
175 @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_BIND_MAX},
|
Chris@16
|
176 @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_LOCALS_AS_TPARAMS}.
|
Chris@16
|
177 */
|
Chris@16
|
178 #define BOOST_LOCAL_FUNCTION(declarations)
|
Chris@16
|
179
|
Chris@16
|
180 /**
|
Chris@16
|
181 @brief This macro is used to start a local function declaration within
|
Chris@16
|
182 templates.
|
Chris@16
|
183
|
Chris@16
|
184 This macro must be used instead of @RefMacro{BOOST_LOCAL_FUNCTION} when
|
Chris@16
|
185 declaring a local function within a template.
|
Chris@16
|
186 A part from that, this macro has the exact same syntax a
|
Chris@16
|
187 @RefMacro{BOOST_LOCAL_FUNCTION} (see @RefMacro{BOOST_LOCAL_FUNCTION} for more
|
Chris@16
|
188 information):
|
Chris@16
|
189 @code
|
Chris@16
|
190 { // Some declarative context within a template.
|
Chris@16
|
191 ...
|
Chris@16
|
192 result_type BOOST_LOCAL_FUNCTION_TPL(declarations) {
|
Chris@16
|
193 ... // Body code.
|
Chris@16
|
194 } BOOST_LOCAL_FUNCTION_NAME_TPL(qualified_name)
|
Chris@16
|
195 ...
|
Chris@16
|
196 }
|
Chris@16
|
197 @endcode
|
Chris@16
|
198
|
Chris@16
|
199 Note that @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL} must be used with this
|
Chris@16
|
200 macro instead of @RefMacro{BOOST_LOCAL_FUNCTION_NAME}.
|
Chris@16
|
201
|
Chris@16
|
202 This macro cannot be portably expanded multiple times on the same line.
|
Chris@16
|
203 In these cases, use the @RefMacro{BOOST_LOCAL_FUNCTION_ID_TPL} macro instead.
|
Chris@16
|
204
|
Chris@16
|
205 @Note C++03 does not allow to use <c>typename</c> outside templates.
|
Chris@16
|
206 This library internally manipulates types, these operations require
|
Chris@16
|
207 <c>typename</c> but only within templates.
|
Chris@16
|
208 This macro is used to indicate to the library when the enclosing scope is a
|
Chris@16
|
209 template so the library can correctly use <c>typename</c>.
|
Chris@16
|
210
|
Chris@16
|
211 @See @RefSect{tutorial, Tutorial} section, @RefMacro{BOOST_LOCAL_FUNCTION},
|
Chris@16
|
212 @RefMacro{BOOST_LOCAL_FUNCTION_ID_TPL},
|
Chris@16
|
213 @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL}.
|
Chris@16
|
214 */
|
Chris@16
|
215 #define BOOST_LOCAL_FUNCTION_TPL(declarations)
|
Chris@16
|
216
|
Chris@16
|
217 /**
|
Chris@16
|
218 @brief This macro allows to declare multiple local functions on the same line.
|
Chris@16
|
219
|
Chris@16
|
220 This macro is equivalent to @RefMacro{BOOST_LOCAL_FUNCTION} but it can be
|
Chris@16
|
221 expanded multiple times on the same line if different identifiers <c>id</c> are
|
Chris@16
|
222 provided for each expansion (see the
|
Chris@16
|
223 @RefSect{advanced_topics, Advanced Topics} section).
|
Chris@16
|
224
|
Chris@16
|
225 @Params
|
Chris@16
|
226 @Param{id,
|
Chris@16
|
227 A unique identifier token which can be concatenated by the preprocessor
|
Chris@16
|
228 (<c>__LINE__</c>\, <c>local_function_number_1_on_line_123</c>\, etc).
|
Chris@16
|
229 }
|
Chris@16
|
230 @Param{declarations,
|
Chris@16
|
231 Same as the <c>declarations</c> parameter of the
|
Chris@16
|
232 @RefMacro{BOOST_LOCAL_FUNCTION} macro.
|
Chris@16
|
233 }
|
Chris@16
|
234 @EndParams
|
Chris@16
|
235
|
Chris@16
|
236 The @RefMacro{BOOST_LOCAL_FUNCTION_NAME} macro should be used to end each one
|
Chris@16
|
237 of the multiple local function declarations as usual (and it will specify a
|
Chris@16
|
238 unique name for each local function).
|
Chris@16
|
239
|
Chris@16
|
240 Within templates, the special macros @RefMacro{BOOST_LOCAL_FUNCTION_ID_TPL}
|
Chris@16
|
241 must be used.
|
Chris@16
|
242
|
Chris@16
|
243 @Note This macro can be useful when the local function macros are expanded
|
Chris@16
|
244 within user-defined macros (because macros all expand on the same line).
|
Chris@16
|
245 On some compilers (e.g., MSVC which supports the non-standard
|
Chris@16
|
246 <c>__COUNTER__</c> macro) it might not be necessary to use this macro but
|
Chris@16
|
247 the use of this macro when expanding multiple local function macros on the same
|
Chris@16
|
248 line is always necessary to ensure portability (this is because this library
|
Chris@16
|
249 can only portably use <c>__LINE__</c> to internally generate unique
|
Chris@16
|
250 identifiers).
|
Chris@16
|
251
|
Chris@16
|
252 @See @RefSect{advanced_topics, Advanced Topics} section,
|
Chris@16
|
253 @RefMacro{BOOST_LOCAL_FUNCTION}, @RefMacro{BOOST_LOCAL_FUNCTION_NAME},
|
Chris@16
|
254 @RefMacro{BOOST_LOCAL_FUNCTION_ID_TPL}.
|
Chris@16
|
255 */
|
Chris@16
|
256 #define BOOST_LOCAL_FUNCTION_ID(id, declarations)
|
Chris@16
|
257
|
Chris@16
|
258 /**
|
Chris@16
|
259 @brief This macro allows to declare multiple local functions on the same line
|
Chris@16
|
260 within templates.
|
Chris@16
|
261
|
Chris@16
|
262 This macro must be used instead of @RefMacro{BOOST_LOCAL_FUNCTION_TPL} when
|
Chris@16
|
263 declaring multiple local functions on the same line within a template.
|
Chris@16
|
264 A part from that, this macro has the exact same syntax as
|
Chris@16
|
265 @RefMacro{BOOST_LOCAL_FUNCTION_TPL} (see @RefMacro{BOOST_LOCAL_FUNCTION_TPL}
|
Chris@16
|
266 for more information).
|
Chris@16
|
267
|
Chris@16
|
268 @Params
|
Chris@16
|
269 @Param{id,
|
Chris@16
|
270 A unique identifier token which can be concatenated by the preprocessor
|
Chris@16
|
271 (<c>__LINE__</c>\, <c>local_function_number_1_on_line_123</c>\, etc).
|
Chris@16
|
272 }
|
Chris@16
|
273 @Param{declarations,
|
Chris@16
|
274 Same as the <c>declarations</c> parameter of the
|
Chris@16
|
275 @RefMacro{BOOST_LOCAL_FUNCTION_TPL} macro.
|
Chris@16
|
276 }
|
Chris@16
|
277 @EndParams
|
Chris@16
|
278
|
Chris@16
|
279 The @RefMacro{BOOST_LOCAL_FUNCTION_NAME} macro should be used to end each one
|
Chris@16
|
280 of the multiple local function declarations as usual (and it will specify a
|
Chris@16
|
281 unique name for each local function).
|
Chris@16
|
282
|
Chris@16
|
283 Outside template, the macro @RefMacro{BOOST_LOCAL_FUNCTION_ID} should be used
|
Chris@16
|
284 to declare multiple local functions on the same line.
|
Chris@16
|
285
|
Chris@16
|
286 @Note This macro can be useful when the local function macros are expanded
|
Chris@16
|
287 within user-defined macros (because macros all expand on the same line).
|
Chris@16
|
288 On some compilers (e.g., MSVC which supports the non-standard
|
Chris@16
|
289 <c>__COUNTER__</c> macro) it might not be necessary to use this macro but
|
Chris@16
|
290 the use of this macro when expanding multiple local function macros on the same
|
Chris@16
|
291 line is always necessary to ensure portability (this is because this library
|
Chris@16
|
292 can only portably use <c>__LINE__</c> to internally generate unique
|
Chris@16
|
293 identifiers).
|
Chris@16
|
294
|
Chris@16
|
295 @See @RefSect{advanced_topics, Advanced Topics} section,
|
Chris@16
|
296 @RefMacro{BOOST_LOCAL_FUNCTION_TPL}, @RefMacro{BOOST_LOCAL_FUNCTION_NAME},
|
Chris@16
|
297 @RefMacro{BOOST_LOCAL_FUNCTION_ID}.
|
Chris@16
|
298 */
|
Chris@16
|
299 #define BOOST_LOCAL_FUNCTION_ID_TPL(id, declarations)
|
Chris@16
|
300
|
Chris@16
|
301 /**
|
Chris@16
|
302 @brief This macro is used to end a local function declaration specifying its
|
Chris@16
|
303 name.
|
Chris@16
|
304
|
Chris@16
|
305 This macro must follow the local function body code block <c>{ ... }</c>:
|
Chris@16
|
306 @code
|
Chris@16
|
307 { // Some declarative context.
|
Chris@16
|
308 ...
|
Chris@16
|
309 result_type BOOST_LOCAL_FUNCTION(declarations) {
|
Chris@16
|
310 ... // Body code.
|
Chris@16
|
311 } BOOST_LOCAL_FUNCTION_NAME(qualified_name)
|
Chris@16
|
312 ...
|
Chris@16
|
313 }
|
Chris@16
|
314 @endcode
|
Chris@16
|
315
|
Chris@16
|
316 Within templates, the special macros @RefMacro{BOOST_LOCAL_FUNCTION_TPL} and
|
Chris@16
|
317 @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL} must be used.
|
Chris@16
|
318
|
Chris@16
|
319 @Params
|
Chris@16
|
320 @Param{qualified_name,
|
Chris@16
|
321 The name of the local function optionally qualified as follow:
|
Chris@16
|
322 @code
|
Chris@16
|
323 name:
|
Chris@16
|
324 [inline] [recursive] local_function_name
|
Chris@16
|
325 @endcode
|
Chris@16
|
326 (Lexical conventions: <c>token1 | token2</c> means either <c>token1</c> or
|
Chris@16
|
327 <c>token2</c>; <c>[token]</c> means either <c>token</c> or nothing;
|
Chris@16
|
328 <c>{expression}</c> means the token resulting from the expression.)
|
Chris@16
|
329 }
|
Chris@16
|
330 @EndParams
|
Chris@16
|
331
|
Chris@16
|
332 The local function name can be qualified by prefixing it with the keyword
|
Chris@16
|
333 <c>inline</c> (see the @RefSect{advanced_topics, Advanced Topics} section):
|
Chris@16
|
334 @code
|
Chris@16
|
335 BOOST_LOCAL_FUNCTION_NAME(inline local_function_name)
|
Chris@16
|
336 @endcode
|
Chris@16
|
337 This increases the chances that the compiler will be able to inline the local
|
Chris@16
|
338 function calls (thus reducing run-time).
|
Chris@16
|
339 However, inline local functions cannot be passed as template parameters (e.g., to <c>std::for_each</c>) or assigned to other functors (e.g., to
|
Chris@16
|
340 <c>boost::function</c>).
|
Chris@16
|
341 That is true on C++03 compilers but inline local functions can instead be
|
Chris@16
|
342 passed as template parameters on C++11 compilers.
|
Chris@16
|
343 On C++11 compilers, there is no need to declare a local function lined because
|
Chris@16
|
344 this library will automatically use C++11 specific features to inline the local
|
Chris@16
|
345 function while always allowing to pass it as a template parameter.
|
Chris@16
|
346 This optimization is automatically enabled when the Boost.Config macro
|
Chris@16
|
347 <c>BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS</c> is not defined but it also be
|
Chris@16
|
348 forced using @RefMacro{BOOST_LOCAL_FUNCTION_CONFIG_LOCALS_AS_TPARAMS}.
|
Chris@16
|
349
|
Chris@16
|
350 The local function name can also be qualified by prefixing it with the
|
Chris@16
|
351 "keyword" <c>recursive</c> (see the
|
Chris@16
|
352 @RefSect{advanced_topics, Advanced Topics} section):
|
Chris@16
|
353 @code
|
Chris@16
|
354 BOOST_LOCAL_FUNCTION_NAME(recursive local_function_name)
|
Chris@16
|
355 @endcode
|
Chris@16
|
356 This allows the local function to recursively call itself from its body (as
|
Chris@16
|
357 usual in C++).
|
Chris@16
|
358 However, recursive local functions should only be called within their
|
Chris@16
|
359 declaration scope (otherwise the result is undefined behaviour).
|
Chris@16
|
360 Finally, compilers have not been observed to be able to inline recursive local
|
Chris@16
|
361 function calls, not even when the recursive local function is also declared
|
Chris@16
|
362 inline:
|
Chris@16
|
363 @code
|
Chris@16
|
364 BOOST_LOCAL_FUNCTION(inline recursive local_function_name)
|
Chris@16
|
365 @endcode
|
Chris@16
|
366
|
Chris@16
|
367 @Note The local function name cannot be the name of an operator
|
Chris@16
|
368 <c>operator...</c> and it cannot be the same name of another local function
|
Chris@16
|
369 declared within the same enclosing scope (but <c>boost::overloaded_function</c>
|
Chris@16
|
370 can be used to overload local functions, see
|
Chris@16
|
371 Boost.Functional/OverloadedFunction and the
|
Chris@16
|
372 @RefSect{advanced_topics, Advanced Topics} section).
|
Chris@16
|
373
|
Chris@16
|
374 @See @RefSect{tutorial, Tutorial} section,
|
Chris@16
|
375 @RefSect{advanced_topics, Advanced Topics} section,
|
Chris@16
|
376 @RefMacro{BOOST_LOCAL_FUNCTION},
|
Chris@16
|
377 @RefMacro{BOOST_LOCAL_FUNCTION_NAME_TPL}.
|
Chris@16
|
378 */
|
Chris@16
|
379 #define BOOST_LOCAL_FUNCTION_NAME(qualified_name)
|
Chris@16
|
380
|
Chris@16
|
381 /**
|
Chris@16
|
382 @brief This macro is used to end a local function declaration specifying its
|
Chris@16
|
383 name within templates.
|
Chris@16
|
384
|
Chris@16
|
385 This macro must be used instead of @RefMacro{BOOST_LOCAL_FUNCTION_NAME} when
|
Chris@16
|
386 declaring a local function within a template.
|
Chris@16
|
387 A part from that, this macro has the exact same syntax a
|
Chris@16
|
388 @RefMacro{BOOST_LOCAL_FUNCTION_NAME} (see @RefMacro{BOOST_LOCAL_FUNCTION_NAME}
|
Chris@16
|
389 for more information):
|
Chris@16
|
390 @code
|
Chris@16
|
391 { // Some declarative context within a template.
|
Chris@16
|
392 ...
|
Chris@16
|
393 result_type BOOST_LOCAL_FUNCTION_TPL(declarations) {
|
Chris@16
|
394 ... // Body code.
|
Chris@16
|
395 } BOOST_LOCAL_FUNCTION_NAME_TPL(qualified_name)
|
Chris@16
|
396 ...
|
Chris@16
|
397 }
|
Chris@16
|
398 @endcode
|
Chris@16
|
399
|
Chris@16
|
400 Note that @RefMacro{BOOST_LOCAL_FUNCTION_TPL} must be used with this macro
|
Chris@16
|
401 instead of @RefMacro{BOOST_LOCAL_FUNCTION}.
|
Chris@16
|
402
|
Chris@16
|
403 @Note C++03 does not allow to use <c>typename</c> outside templates.
|
Chris@16
|
404 This library internally manipulates types, these operations require
|
Chris@16
|
405 <c>typename</c> but only within templates.
|
Chris@16
|
406 This macro is used to indicate to the library when the enclosing scope is a
|
Chris@16
|
407 template so the library can correctly use <c>typename</c>.
|
Chris@16
|
408
|
Chris@16
|
409 @See @RefSect{tutorial, Tutorial} section,
|
Chris@16
|
410 @RefMacro{BOOST_LOCAL_FUNCTION_NAME}, @RefMacro{BOOST_LOCAL_FUNCTION_TPL}.
|
Chris@16
|
411 */
|
Chris@16
|
412 #define BOOST_LOCAL_FUNCTION_NAME_TPL(name)
|
Chris@16
|
413
|
Chris@16
|
414 /**
|
Chris@16
|
415 @brief This macro expands to the type of the specified bound variable.
|
Chris@16
|
416
|
Chris@16
|
417 This macro can be used within the local functions body to refer to the bound
|
Chris@16
|
418 variable types so to declare local variables, check concepts (using
|
Chris@16
|
419 Boost.ConceptCheck), etc (see the @RefSect{advanced_topics, Advanced Topics}
|
Chris@16
|
420 section).
|
Chris@16
|
421 This way the local function can be programmed entirely without explicitly
|
Chris@16
|
422 specifying the bound variable types thus facilitating maintenance (e.g., if
|
Chris@16
|
423 the type of a bound variable changes in the enclosing scope, the local function
|
Chris@16
|
424 code does not have to change).
|
Chris@16
|
425
|
Chris@16
|
426 @Params
|
Chris@16
|
427 @Param{bound_variable_name,
|
Chris@16
|
428 The name of one of the local function's bound variables.
|
Chris@16
|
429 }
|
Chris@16
|
430 @EndParams
|
Chris@16
|
431
|
Chris@16
|
432 The type returned by the macro is fully qualified in that it contains the extra
|
Chris@16
|
433 constant and reference qualifiers when the specified variable is bound by
|
Chris@16
|
434 constant and by reference.
|
Chris@16
|
435 For example, if a variable named <c>t</c> of type <c>T</c> is:
|
Chris@16
|
436 @li Bound by value using <c>bind t</c> then
|
Chris@16
|
437 <c>BOOST_LOCAL_FUNCTION_TYPEOF(t)</c> is <c>T</c>.
|
Chris@16
|
438 @li Bound by constant value using <c>const bind t</c> then
|
Chris@16
|
439 <c>BOOST_LOCAL_FUNCTION_TYPEOF(t)</c> is <c>const T</c>.
|
Chris@16
|
440 @li Bound by reference using <c>bind& t</c> then
|
Chris@16
|
441 <c>BOOST_LOCAL_FUNCTION_TYPEOF(t)</c> is <c>T&</c>.
|
Chris@16
|
442 @li Bound by constant reference using <c>const bind& t</c> then
|
Chris@16
|
443 <c>BOOST_LOCAL_FUNCTION_TYPEOF(t)</c> is <c>const T&</c>.
|
Chris@16
|
444
|
Chris@16
|
445 This macro must be prefixed by <c>typename</c> when used within templates.
|
Chris@16
|
446
|
Chris@16
|
447 @Note It is best to use this macro instead of Boost.Typeof so to reduce the
|
Chris@16
|
448 number of times Boost.Typeof is used to deduce types (see the
|
Chris@16
|
449 @RefSect{advanced_topics, Advanced Topics} section).
|
Chris@16
|
450
|
Chris@16
|
451 @See @RefSect{advanced_topics, Advanced Topics} section,
|
Chris@16
|
452 @RefMacro{BOOST_LOCAL_FUNCTION}.
|
Chris@16
|
453 */
|
Chris@16
|
454 #define BOOST_LOCAL_FUNCTION_TYPEOF(bound_variable_name)
|
Chris@16
|
455
|
Chris@16
|
456 #endif // DOXYGEN
|
Chris@16
|
457
|
Chris@16
|
458 #endif // #include guard
|
Chris@16
|
459
|