]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/local_function/test/factorial.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / local_function / test / factorial.cpp
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/typeof/typeof.hpp>
15 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
16 #include <boost/detail/lightweight_test.hpp>
17 #include <algorithm>
18 #include <vector>
19
20 struct calculator;
21 BOOST_TYPEOF_REGISTER_TYPE(calculator) // Register before `bind this_` below.
22
23 //[factorial
24 struct calculator {
25 std::vector<int> results;
26
27 void factorials(const std::vector<int>& nums) {
28 int BOOST_LOCAL_FUNCTION(bind this_, int num,
29 bool recursion, default false) {
30 int result = 0;
31
32 if(num <= 0) result = 1;
33 else result = num * factorial(num - 1, true); // Recursive call.
34
35 if(!recursion) this_->results.push_back(result);
36 return result;
37 } BOOST_LOCAL_FUNCTION_NAME(recursive factorial) // Recursive.
38
39 std::for_each(nums.begin(), nums.end(), factorial);
40 }
41 };
42 //]
43
44 int main(void) {
45 std::vector<int> v(3);
46 v[0] = 1; v[1] = 3; v[2] = 4;
47
48 calculator calc;
49 calc.factorials(v);
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();
54 }
55
56 #endif // VARIADIC_MACROS
57