C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
chapter-7.cpp
Go to the documentation of this file.
1// ===-- chapter-7/chapter-7.cpp -------------------------- -*- C++ -*- --=== //
9#include "exercise-7-0.hpp"
10#include "exercise-7-2.hpp"
11#include "exercise-7-3.hpp"
12#include "exercise-7-7.hpp"
13
15
16#include <boost/mpl/equal.hpp>
17#include <boost/mpl/list.hpp>
18#include <boost/mpl/list_c.hpp>
19#include <boost/mpl/range_c.hpp>
20#include <boost/mpl/vector.hpp>
21#include <boost/mpl/vector_c.hpp>
22#include <boost/static_assert.hpp>
23
24
109namespace test_7_2 {
110
111typedef boost::mpl::vector_c<int, 1, 3, 5, 7> v1;
112typedef boost::mpl::vector_c<int, 2, 4, 6, 8> v2;
113typedef boost::mpl::vector<v1, v2> zip_vec;
117
119 boost::is_same<
121 boost::mpl::random_access_iterator_tag
122 >::value));
123
124typedef boost::mpl::begin<zip_vec_t>::type zv_begin;
125typedef boost::mpl::deref<zv_begin>::type zv_begin_t;
127 boost::mpl::equal<
128 zv_begin_t, boost::mpl::vector_c<int, 1, 2>
129 >::value));
130typedef boost::mpl::next<zv_begin>::type zv_next;
132 boost::mpl::equal<
133 boost::mpl::deref<zv_next>::type, boost::mpl::vector_c<int, 3, 4>
134 >::value));
135typedef boost::mpl::prior<zv_next>::type zv_prior;
136BOOST_STATIC_ASSERT((boost::is_same<zv_prior, zv_begin>::value));
138 boost::mpl::equal<
139 boost::mpl::deref<zv_prior>::type, boost::mpl::vector_c<int, 1, 2>
140 >::value));
141typedef boost::mpl::at< zip_vec_t, boost::mpl::int_<2> >::type zv_at2;
143 boost::mpl::equal<
144 boost::mpl::deref<zv_at2>::type, boost::mpl::vector_c<int, 5, 6>
145 >::value));
146
147typedef boost::mpl::advance< zv_next, boost::mpl::int_<2> >::type zv_at3;
149 boost::mpl::equal<
150 boost::mpl::deref<zv_at3>::type, boost::mpl::vector_c<int, 7, 8>
151 >::value));
152
153typedef boost::mpl::list_c<int, 1, 3, 5, 7> l1;
154typedef boost::mpl::list_c<int, 2, 4, 6, 8> l2;
155typedef boost::mpl::list<l1, l2> zip_list;
159 boost::is_same<
161 boost::mpl::forward_iterator_tag
162 >::value));
163
164} // namespace test_7_2
165
166
169namespace test_7_3 {
170
171typedef boost::mpl::vector_c<int,5,6,7,8,9,0,1,2,3,4> v;
173 v,
174 boost::mpl::advance_c<boost::mpl::begin<v>::type, 5>::type
176
178 boost::mpl::equal< view, boost::mpl::range_c<int, 0, 10> >::value));
179
180} // namespace test_7_3
181
182
185namespace test_7_6 {
186using namespace chapter7;
187
188typedef permutation_view<
189 boost::mpl::list_c<int,2,1,3,0,2>, // indices
190 boost::mpl::vector_c<int,11,22,33,44> // elements
192
193typedef boost::mpl::begin<pv1>::type it0;
194typedef boost::mpl::deref<it0>::type val0;
195BOOST_STATIC_ASSERT((33 == val0::value));
196
197typedef boost::mpl::next<it0>::type it1;
198typedef boost::mpl::deref<it1>::type val1;
199BOOST_STATIC_ASSERT((22 == val1::value));
200
202 boost::mpl::equal< pv1, boost::mpl::vector_c<int,33,22,44,11,33> >::value));
203
204} // namespace test_7_6
205
206
209namespace test_7_7 {
210
211typedef boost::mpl::vector_c<int, 1, 2> v1;
213typedef boost::mpl::deref<ri0>::type rv0;
214BOOST_STATIC_ASSERT((rv0::value == 2));
215
216typedef boost::mpl::next<ri0>::type ri1;
217typedef boost::mpl::deref<ri1>::type rv1;
218BOOST_STATIC_ASSERT((rv1::value == 1));
219
220typedef boost::mpl::next<ri1>::type ri2;
222BOOST_STATIC_ASSERT((boost::is_same<ri2, ri_end>::value));
223
224typedef boost::mpl::prior<ri1>::type ri1_prior;
225BOOST_STATIC_ASSERT((boost::is_same<ri1_prior, ri0>::value));
226
227} // namespace test_7_7
228
229
230int main()
231{
232 return 0;
233}
Defines a view of a sequence that appears to alter the elements' order.
int main()
Definition: chapter-7.cpp:230
Solution to Exercise 7-0.
Solution to Exercise 7-2.
Solution to Exercise 7-3.
Solution to Exercise 7-7.
Provide utilities availble to all Chapter 7 solutions.
Tests functionality from Exercise 7-2.
Definition: chapter-7.cpp:109
boost::mpl::at< zip_vec_t, boost::mpl::int_< 2 > >::type zv_at2
Definition: chapter-7.cpp:141
BOOST_STATIC_ASSERT((boost::is_same< zip_vec_category, boost::mpl::random_access_iterator_tag >::value))
chapter7::least_refined_iterator_category< zip_vec >::type zip_vec_category
Definition: chapter-7.cpp:116
boost::mpl::vector_c< int, 1, 3, 5, 7 > v1
Definition: chapter-7.cpp:111
boost::mpl::next< zv_begin >::type zv_next
Definition: chapter-7.cpp:130
boost::mpl::list_c< int, 1, 3, 5, 7 > l1
Definition: chapter-7.cpp:153
chapter7::zip_view< zip_vec > zip_vec_t
Definition: chapter-7.cpp:114
boost::mpl::list_c< int, 2, 4, 6, 8 > l2
Definition: chapter-7.cpp:154
boost::mpl::advance< zv_next, boost::mpl::int_< 2 > >::type zv_at3
Definition: chapter-7.cpp:147
boost::mpl::vector_c< int, 2, 4, 6, 8 > v2
Definition: chapter-7.cpp:112
boost::mpl::vector< v1, v2 > zip_vec
Definition: chapter-7.cpp:113
boost::mpl::begin< zip_vec_t >::type zv_begin
Definition: chapter-7.cpp:124
boost::mpl::list< l1, l2 > zip_list
Definition: chapter-7.cpp:155
boost::mpl::deref< zv_begin >::type zv_begin_t
Definition: chapter-7.cpp:125
boost::mpl::prior< zv_next >::type zv_prior
Definition: chapter-7.cpp:135
chapter7::least_refined_iterator_category< zip_list >::type zip_list_category
Definition: chapter-7.cpp:157
Tests functionality from Exercise 7-3.
Definition: chapter-7.cpp:169
BOOST_STATIC_ASSERT((boost::mpl::equal< view, boost::mpl::range_c< int, 0, 10 > >::value))
exercise_7_3::rotate_view< v, boost::mpl::advance_c< boost::mpl::begin< v >::type, 5 >::type > view
Definition: chapter-7.cpp:175
boost::mpl::vector_c< int, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4 > v
Definition: chapter-7.cpp:171
Tests functionality from Exercise 7-6.
Definition: chapter-7.cpp:185
boost::mpl::deref< it1 >::type val1
Definition: chapter-7.cpp:198
boost::mpl::next< it0 >::type it1
Definition: chapter-7.cpp:197
permutation_view< boost::mpl::list_c< int, 2, 1, 3, 0, 2 >, boost::mpl::vector_c< int, 11, 22, 33, 44 > > pv1
Definition: chapter-7.cpp:191
boost::mpl::deref< it0 >::type val0
Definition: chapter-7.cpp:194
boost::mpl::begin< pv1 >::type it0
Definition: chapter-7.cpp:193
BOOST_STATIC_ASSERT((33==val0::value))
Tests functionality from Exercise 7-7.
Definition: chapter-7.cpp:209
boost::mpl::vector_c< int, 1, 2 > v1
Definition: chapter-7.cpp:211
boost::mpl::rend< v1 >::type ri_end
Definition: chapter-7.cpp:221
boost::mpl::next< ri0 >::type ri1
Definition: chapter-7.cpp:216
boost::mpl::prior< ri1 >::type ri1_prior
Definition: chapter-7.cpp:224
BOOST_STATIC_ASSERT((rv0::value==2))
boost::mpl::next< ri1 >::type ri2
Definition: chapter-7.cpp:220
boost::mpl::deref< ri1 >::type rv1
Definition: chapter-7.cpp:217
boost::mpl::rbegin< v1 >::type ri0
Definition: chapter-7.cpp:212
boost::mpl::deref< ri0 >::type rv0
Definition: chapter-7.cpp:213
boost::mpl::if_< boost::mpl::empty< non_random_access_iterators >, boost::mpl::random_access_iterator_tag, typenameboost::mpl::if_< boost::mpl::empty< non_bidirectional_iterators >, boost::mpl::bidirectional_iterator_tag, boost::mpl::forward_iterator_tag >::type >::type type
Alter the order sequence traversal using a sequence of positions.
Wraps corresponding elements across a sequence of sequences.
A shifted and wrapped view onto the original sequence.
Wraps any iterator with the intent of reversing the direction of next and prior.