C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
exercise-4-3.hpp
Go to the documentation of this file.
1// ===-- chapter-4/exercise-4-3.hpp ----------------------- -*- C++ -*- --=== //
9#ifndef EXERCISE_4_3
10#define EXERCISE_4_3
11
12#include <boost/static_assert.hpp>
13
14#include <boost/mpl/apply.hpp>
15#include <boost/mpl/if.hpp>
16#include <boost/mpl/greater.hpp>
17#include <boost/mpl/identity.hpp>
18#include <boost/mpl/minus.hpp>
19#include <boost/mpl/multiplies.hpp>
20#include <boost/mpl/not_equal_to.hpp>
21#include <boost/mpl/plus.hpp>
22
23#include <boost/mpl/placeholders.hpp>
24using namespace boost::mpl::placeholders;
25
26// Convenience used throughout the book's examples...
27namespace mpl = boost::mpl;
28
29
68namespace exercise_4_3 {
69
71template <typename N, typename Predicate>
72struct next_if
73 : mpl::eval_if<
74 typename mpl::apply<Predicate, N>::type, // Why do I need ::type here?
75 mpl::next<N>,
76 mpl::identity<N>
77 >::type
78{ };
79
81template <typename N1, typename N2>
82struct formula
83 : mpl::eval_if<
84 mpl::not_equal_to<N1,N2>,
85 mpl::eval_if<
86 mpl::greater<N1,N2>,
87 mpl::minus<N1,N2>,
88 mpl::identity<N1> // Could skip identity...
89 >,
90 mpl::plus<
91 N1,
92 mpl::multiplies< N1, mpl::int_<2> >
93 >
94 >::type
95{ };
96
97} // namespace exercise_4_3
98
99#endif // EXERCISE_4_3
Exists to inject functionality into the Boost MPL namespace.
Encapsulate solution for Exercise 4-3.
Fix algorithm 2 in Exercise 4-3:
Fix algorithm 1 in Exercise 4-3: