C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
exercise-5-6.hpp
Go to the documentation of this file.
1// ===-- chapter-5/exercise-5-6.hpp ----------------------- -*- C++ -*- --=== //
9#ifndef EXERCISE_5_6
10#define EXERCISE_5_6
11
12#include <boost/mpl/begin_end.hpp>
13#include <boost/mpl/deref.hpp>
14#include <boost/mpl/iterator_tags.hpp>
15#include <boost/mpl/next_prior.hpp>
16
17#include <boost/type_traits/is_array.hpp>
18#include <boost/type_traits/is_same.hpp>
19
20
44namespace exercise_5_6 {
45
48
59template <typename T>
60struct dimensions : boost::mpl::void_ { };
61
64template <typename T, size_t N>
65struct dimensions<T[N]>
66{
67 static size_t const value = N;
68
70
72 typedef typename boost::mpl::if_<
73 boost::is_array<T>,
75 boost::mpl::void_
76 >::type tail;
77
79};
80
82template <typename S>
84{
85 typedef boost::mpl::forward_iterator_tag category;
86};
87
88} // namespace exercise_5_6
89
90
91namespace boost {
92namespace mpl {
93
96
98template <>
99struct begin_impl<exercise_5_6::dimensions_tag>
100{
101 template <typename S>
102 struct apply
103 {
105 };
106};
108template <>
109struct end_impl<exercise_5_6::dimensions_tag>
110{
111 template <typename S>
112 struct apply
113 {
115 };
116};
117
119template <typename S>
120struct deref< exercise_5_6::dimensions_iterator<S> >
121{
122 typedef S type;
123};
124
126template <typename S>
127struct next< exercise_5_6::dimensions_iterator<S> >
128{
129 typedef typename boost::mpl::if_<
130 boost::is_same<boost::mpl::void_, S>,
133};
134
136
137} // namespace mpl
138} // namespace boost
139
140#endif // EXERCISE_5_6
Exists to inject functionality into the Boost MPL namespace.
Exists to inject functionality into the Boost namespace.
Encapsulate solution for Exercise 5-6.
exercise_5_6::dimensions_iterator< boost::mpl::void_ > type
boost::mpl::if_< boost::is_same< boost::mpl::void_, S >, exercise_5_6::dimensions_iterator< boost::mpl::void_ >, exercise_5_6::dimensions_iterator< typenameS::tail > >::type type
boost::mpl::if_< boost::is_array< T >, dimensions< T >, boost::mpl::void_ >::type tail
The rest of the sequence.
Define a (forward-only) iterator for our dimensions sequence.
boost::mpl::forward_iterator_tag category
A tag for tag-dispatched sequence metafunctions.
Represent the dimensions in an array type as a sequence of numbers.