3rdParty/boost/1.78.0/libs/local_function/doc/html/index.html
| | Home | Libraries | People | FAQ | More |
<[email protected]>Copyright © 2009-2012 Lorenzo Caminiti
Distributed under the Boost Software License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt)
This library allows to program functions locally, within other functions, and directly within the scope where they are needed.
Local functions (a.k.a., nested functions) are a form of information hiding and they are useful for dividing procedural tasks into subtasks which are only meaningful locally, avoiding cluttering other parts of the program with functions, variables, etc unrelated to those parts. Therefore, local functions complement other structuring possibilities such as namespaces and classes. Local functions are a feature of many programming languages, notably Pascal and Ada, yet lacking from C++03 (see also [N2511]).
Using C++11 lambda functions, it is possible to implement local functions by naming lambda functions assigning them to local variables. For example (see also add_cxx11_lambda.cpp):
intmain(void){// Some local scope.intsum=0,factor=10;// Variables in scope to bind.autoadd=[factor,&sum](intnum){// C++11 only.sum+=factor\*num;};add(1);// Call the lambda.intnums[]={2,3};std::for\_each(nums,nums+2,add);// Pass it to an algorithm.BOOST\_TEST(sum==60);// Assert final summation value.returnboost::report\_errors();}
This library allows to program local functions portably between C++03 and C++11 (and with performances comparable to lambda functions on C++11 compilers). For example (see also add.cpp):
intmain(void){// Some local scope.intsum=0,factor=10;// Variables in scope to bind.voidBOOST\_LOCAL\_FUNCTION(constbindfactor,bind&sum,intnum){sum+=factor\*num;}BOOST\_LOCAL\_FUNCTION\_NAME(add)add(1);// Call the local function.intnums[]={2,3};std::for\_each(nums,nums+2,add);// Pass it to an algorithm.BOOST\_TEST(sum==60);// Assert final summation value.returnboost::report\_errors();}
This library supports the following features for local functions:
See the Alternatives section for a comparison between this library, C++11 lambda functions, Boost.Phoenix, and other C++ techniques that implement features related to local functions.
[1] This is a strength with respect to C++03 functors implemented using local classes which cannot be passed as template parameters (see [N2657] and the Alternatives section).
[2] This is a weakness with respect to C++11 lambda functions which can instead be specified also within expressions (see the Alternatives section).
|
Last revised: December 02, 2021 at 06:50:25 GMT
|
|