• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright (C) 2009-2012 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0
4 // (see accompanying file LICENSE_1_0.txt or a copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // Home at http://www.boost.org/libs/local_function
7 
8 #include <boost/config.hpp>
9 #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
10 #   error "variadic macros required"
11 #else
12 
13 #include <boost/local_function.hpp>
14 #include <boost/detail/lightweight_test.hpp>
15 #include <algorithm>
16 
17 //[add_template
18 template<typename T>
total(const T & x,const T & y,const T & z)19 T total(const T& x, const T& y, const T& z) {
20     T sum = T(), factor = 10;
21 
22     // Must use the `..._TPL` macros within templates.
23     T BOOST_LOCAL_FUNCTION_TPL(const bind factor, bind& sum, T num) {
24         return sum += factor * num;
25     } BOOST_LOCAL_FUNCTION_NAME_TPL(add)
26 
27     add(x);
28     T nums[2]; nums[0] = y; nums[1] = z;
29     std::for_each(nums, nums + 2, add);
30 
31     return sum;
32 }
33 //]
34 
main(void)35 int main(void) {
36     BOOST_TEST(total(1, 2, 3) == 60);
37     return boost::report_errors();
38 }
39 
40 #endif // VARIADIC_MACROS
41 
42