]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/local_function/test/factorial.cpp
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
8 #include <boost/config.hpp>
9 #ifdef BOOST_NO_CXX11_VARIADIC_MACROS
10 # error "variadic macros required"
13 #include <boost/local_function.hpp>
14 #include <boost/typeof/typeof.hpp>
15 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
16 #include <boost/detail/lightweight_test.hpp>
21 BOOST_TYPEOF_REGISTER_TYPE(calculator
) // Register before `bind this_` below.
25 std::vector
<int> results
;
27 void factorials(const std::vector
<int>& nums
) {
28 int BOOST_LOCAL_FUNCTION(bind this_
, int num
,
29 bool recursion
, default false) {
32 if(num
<= 0) result
= 1;
33 else result
= num
* factorial(num
- 1, true); // Recursive call.
35 if(!recursion
) this_
->results
.push_back(result
);
37 } BOOST_LOCAL_FUNCTION_NAME(recursive factorial
) // Recursive.
39 std::for_each(nums
.begin(), nums
.end(), factorial
);
45 std::vector
<int> v(3);
46 v
[0] = 1; v
[1] = 3; v
[2] = 4;
50 BOOST_TEST(calc
.results
[0] == 1);
51 BOOST_TEST(calc
.results
[1] == 6);
52 BOOST_TEST(calc
.results
[2] == 24);
53 return boost::report_errors();
56 #endif // VARIADIC_MACROS