]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/local_function/example/phoenix_factorial.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / local_function / example / phoenix_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/phoenix/core.hpp>
9 #include <boost/phoenix/function.hpp>
10 #include <boost/detail/lightweight_test.hpp>
11
12 //[phoenix_factorial
13 struct factorial_impl { // Phoenix function from global functor.
14 template<typename Sig>
15 struct result;
16
17 template<typename This, typename Arg>
18 struct result<This (Arg)> : result<This (Arg const&)> {};
19
20 template<typename This, typename Arg>
21 struct result<This (Arg&)> { typedef Arg type; };
22
23 template<typename Arg> // Polymorphic.
24 Arg operator()(Arg n) const {
25 return (n <= 0) ? 1 : n * (*this)(n - 1);
26 }
27 };
28
29 int main(void) {
30 using boost::phoenix::arg_names::arg1;
31
32 boost::phoenix::function<factorial_impl> factorial;
33
34 int i = 4;
35 BOOST_TEST(factorial(i)() == 24); // Call.
36 BOOST_TEST(factorial(arg1)(i) == 24); // Lazy call.
37 return boost::report_errors();
38 }
39 //]
40