C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
exercise-6-1.hpp
Go to the documentation of this file.
1// ===-- chapter-6/exercise-6-1.hpp ----------------------- -*- C++ -*- --=== //
9#ifndef EXERCISE_6_1
10#define EXERCISE_6_1
11
12#include <boost/static_assert.hpp>
13
14#include <boost/mpl/accumulate.hpp>
15#include <boost/mpl/begin_end.hpp>
16#include <boost/mpl/deref.hpp>
17#include <boost/mpl/iterator_tags.hpp>
18#include <boost/mpl/next_prior.hpp>
19#include <boost/mpl/placeholders.hpp>
20#include <boost/mpl/plus.hpp>
21#include <boost/mpl/times.hpp>
22using namespace boost::mpl::placeholders;
23
24
54namespace exercise_6_1 {
55
57struct binary_tag { };
58
59// Forward declaration for the benefit of the MPL sequence algorithms...
60template <unsigned long N> struct binary;
61
62} // namespace exercise_6_1
63
64
65namespace boost {
66namespace mpl {
67
70
72template <unsigned long N>
73struct next< exercise_6_1::binary<N> >
74{
76};
77
79template <unsigned long N>
80struct deref< exercise_6_1::binary<N> >
81{
83};
84
86template <>
87struct begin_impl<exercise_6_1::binary_tag>
88{
89 template <typename T> struct apply { };
90
91 template <unsigned long N>
92 struct apply< exercise_6_1::binary<N> >
93 {
95 };
96};
97
99template <>
100struct end_impl<exercise_6_1::binary_tag>
101{
102 template <typename T> struct apply { };
103
104 template <unsigned long N>
105 struct apply< exercise_6_1::binary<N> >
106 {
108 };
109};
110
112
113} // namespace mpl
114} // namespace boost
115
116
117namespace exercise_6_1 {
118
130template <unsigned long N>
131struct binary
132{
134
135 // Reverse iteration is trivial, but not useful at the moment.
136 typedef boost::mpl::forward_iterator_tag category;
137
144 template <typename T1, typename T2> struct apply_binary_digit { };
145
147 template <typename T1, unsigned long N2>
148 struct apply_binary_digit< T1, binary<N2> >
149 {
150 BOOST_STATIC_ASSERT((N2 % 10 < 2));
151 typedef boost::mpl::plus<
152 boost::mpl::times< T1, boost::mpl::int_<2> >,
153 boost::mpl::int_<N2 % 10>
155 };
156
158 typedef typename boost::mpl::accumulate<
159 binary<N>,
160 boost::mpl::int_<0>,
162
164 static int const value = type::value;
165};
166
167} // namespace exercise_6_1
168
169#endif // EXERCISE_6_1
Exists to inject functionality into the Boost MPL namespace.
Exists to inject functionality into the Boost namespace.
Encapsulate solution for Exercise 6-1.
boost::mpl::plus< boost::mpl::times< T1, boost::mpl::int_< 2 > >, boost::mpl::int_< N2 % 10 > > type
Mathematics used by accumulate is broken out for clarity...
A tag for tag-dispatched sequence metafunctions.
Compile-time binary to decimal number translation.
boost::mpl::forward_iterator_tag category
static int const value
The computed result as a constant value.
boost::mpl::accumulate< binary< N >, boost::mpl::int_< 0 >, apply_binary_digit< _, _ > >::type type
Iterate across the digits, building a decimal result.