[Boost] BOOST_LOCAL_FUNCTION experience

Source: Internet
Author: User

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 == );                        }


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.