In c ++ 11, lambda can be used to define local nested functions in the function. Defining partial functions of some algorithms as local functions can make the code clearer, at the same time, the Declaration and call proximity make maintenance easier. Unfortunately, the company's development platform remains in vs2008, and using the lambda expression of the boost library to simulate it is a bit clumsy and obscure.
The BOOST_LOCAL_FUNCTION macro was introduced after boost1.50 was accidentally seen on the Forum. The official introduction is as follows:
Http://www.boost.org/doc/libs/1_54_0/libs/local_function/doc/html/boost_localfunction/tutorial.html
Local Functions
Local functions are defined using macros from the header fileboost/local_function.hpp. The macros must be used from within a declarative context (this is a limitation with respect to C ++ 11 lambda funich which can instead be declared also within expressions ):
# Include <boost/local_function.hpp> // This library header... {// Some declarative context ....Result-typeBOOST_LOCAL_FUNCTION (Parameters){Body-code} BOOST_LOCAL_FUNCTION_NAME (Name)...}
Using macros to define a nested function (although it only looks like) also gives us another option. BOOST_LOCAL_FUNCTION is easy to use. The official sample code is as follows:
main() { sum = , factor = ; BOOST_LOCAL_FUNCTION( bind factor, bind& sum, sum += factor * add(); nums[] = {, std::for_each(nums, nums + , add); BOOST_TEST(sum == ); }