【boost】BOOST_LOCAL_FUNCTION体验   
               添加时间:2013-7-30 点击量: 
 
              c++11里支撑应用lambda在函数内定义本地嵌套函数,将一些算法的断定式定义为本地函数可以使代码加倍清楚,同时声明和调用接近也使得更轻易保护。遗憾的是公司开辟平台任然逗留在vs2008,应用boost库的lambda表达式来模仿其实是有些笨拙与晦涩。
无意在论坛上看见boost1.50版本后引入了BOOST_LOCAL_FUNCTION宏,官方简介如下:
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  the header file boost/local_function.hpp. The macros must be used  within a declarative context (this is a limitation with respect to C++11 lambda functions which can instead be declared also within expressions):
#include <boost/local_function.hpp> // This library header.
...
{ // Some declarative context.
    ...
    result-type BOOST_LOCAL_FUNCTION(parameters) {
        body-code
    } BOOST_LOCAL_FUNCTION_NAME(name)
    ...
}
应用宏的体式格式来定义了一个嵌套的函数(固然只是看起来像),然则也使得我们有另一种选择。BOOST_LOCAL_FUNCTION应用很是简单,官方示例代码如下:
 1 int main(void) {                            // Some local scope.
 2     int sum = 0, factor = 10;               // Variables in scope to bind.
 3 
 4     void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
 5         sum += factor  num;
 6     } BOOST_LOCAL_FUNCTION_NAME(add)
 7 
 8     add(1);                                 // Call the local function.
 9     int nums[] = {2, 3};
10     std::for_each(nums, nums + 2, add);     // Pass it to an algorithm.
11 
12     BOOST_TEST(sum == 60);                  // Assert final summation value.
13     return boost::report_errors();
14 }
 
 
                     
                  
     
  
 
    
    
c++11里支撑应用lambda在函数内定义本地嵌套函数,将一些算法的断定式定义为本地函数可以使代码加倍清楚,同时声明和调用接近也使得更轻易保护。遗憾的是公司开辟平台任然逗留在vs2008,应用boost库的lambda表达式来模仿其实是有些笨拙与晦涩。
无意在论坛上看见boost1.50版本后引入了BOOST_LOCAL_FUNCTION宏,官方简介如下:
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  the header file boost/local_function.hpp. The macros must be used  within a declarative context (this is a limitation with respect to C++11 lambda functions which can instead be declared also within expressions):
#include <boost/local_function.hpp> // This library header.
...
{ // Some declarative context.
...
result-type BOOST_LOCAL_FUNCTION(parameters) {
body-code
} BOOST_LOCAL_FUNCTION_NAME(name)
...
}
应用宏的体式格式来定义了一个嵌套的函数(固然只是看起来像),然则也使得我们有另一种选择。BOOST_LOCAL_FUNCTION应用很是简单,官方示例代码如下:
1 int main(void) { // Some local scope.
2 int sum = 0, factor = 10; // Variables in scope to bind.
3
4 void BOOST_LOCAL_FUNCTION(const bind factor, bind& sum, int num) {
5 sum += factor num;
6 } BOOST_LOCAL_FUNCTION_NAME(add)
7
8 add(1); // Call the local function.
9 int nums[] = {2, 3};
10 std::for_each(nums, nums + 2, add); // Pass it to an algorithm.
11
12 BOOST_TEST(sum == 60); // Assert final summation value.
13 return boost::report_errors();
14 }
 




