C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
chapter-6.cpp
Go to the documentation of this file.
1// ===-- chapter-6/chapter-6.cpp -------------------------- -*- C++ -*- --=== //
9#include "exercise-6-0.hpp"
10#include "exercise-6-1.hpp"
11#include "exercise-6-3.hpp"
12#include "exercise-6-4.hpp"
13
14#include <boost/mpl/copy.hpp>
15#include <boost/mpl/equal.hpp>
16#include <boost/mpl/vector_c.hpp>
17#include <boost/type_traits/is_same.hpp>
18
19#include <stdio.h>
20
21#ifdef EXERCISE_6_3
22# include "exercise-5-10.hpp"
23#endif
24
25
73namespace {
74
77namespace test_6_0 {
78using namespace exercise_6_0;
79
81 boost::is_same<
82 smallest< boost::mpl::vector<int[2], char, double&> >::type,
83 char
84 >::value
85));
87 boost::is_same<
88 smallest< boost::mpl::vector<int, char[20], double&> >::type,
89 int
90 >::value
91));
93 boost::is_same<smallest< boost::mpl::vector<> >::type, no_type>::value
94));
95
96} // namespace test_6_0
97
98
101namespace test_6_3 {
102using namespace exercise_6_3;
103
105typedef chapter5::tree<
106 mpl_::integral_c<int, 17>,
108 mpl_::integral_c<int, 10>,
109 mpl_::integral_c<int, 2>,
110 mpl_::integral_c<int, 11> >,
111 mpl_::integral_c<int, 25>
113BOOST_STATIC_ASSERT((boost::is_same<exercise_6_3::bst, bst_expected>::value));
114
116 boost::mpl::equal<
118 boost::mpl::vector_c<int, 2, 10, 11, 17, 25>
119 >::value));
120
121} // namespace test_6_3
122
123
126namespace test_6_4 {
127using chapter5::tree;
128using namespace exercise_6_3;
129using namespace exercise_6_4;
130
131typedef boost::mpl::copy<
132 boost::mpl::vector_c<int, 17, 25, 10, 2, 11>,
134>::type bst;
138typedef boost::mpl::end<bst>::type end_pos;
139
140// Note: equal means int_<11> == int_<20> by some miracle -- equal_to is
141// correct.
143 !boost::mpl::equal_to<
144 boost::mpl::int_<11>,
145 boost::mpl::int_<20>
146 >::value));
147
148BOOST_STATIC_ASSERT((!boost::is_same<pos1, end_pos>::value));
149BOOST_STATIC_ASSERT(( boost::is_same<pos2, end_pos>::value));
150
151} // namespace test_6_4
152
153
156static void test_exercise_6_1()
157{
158#ifdef EXERCISE_6_1
159 using namespace exercise_6_1;
160 unsigned const zero = binary<0>::value;
161 unsigned const one = binary<1>::value;
162 unsigned const three = binary<11>::value;
163 unsigned const five = binary<101>::value;
164 unsigned const seven = binary<111>::value;
165 unsigned const nine = binary<1001>::value;
166
167 printf("Zero: %i\n", zero);
168 printf("One: %i\n", one);
169 printf("Three: %i\n", three);
170 printf("Five: %i\n", five);
171 printf("Seven: %i\n", seven);
172 printf("Nine: %i\n", nine);
173#endif
174}
175
176} // namespace <anon>
177
178
179int main()
180{
182 return 0;
183}
BOOST_STATIC_ASSERT((boost::is_same< t5, expected_t5 >::value))
int main()
Definition: chapter-6.cpp:179
Solution to Exercise 5-10.
Solution to Exercise 6-0.
Solution to Exercise 6-1.
Solution to Exercise 6-3.
Solution to Exercise 6-4.
static void test_exercise_6_1()
Tests for Exercise 6-1.
Definition: chapter-6.cpp:156
binary_tree_search< bst, boost::mpl::int_< 20 > >::type pos2
Definition: chapter-6.cpp:137
binary_tree_search< bst, boost::mpl::int_< 11 > >::type pos1
Definition: chapter-6.cpp:136
boost::mpl::copy< boost::mpl::vector_c< int, 17, 25, 10, 2, 11 >, binary_tree_inserter< tree<> > >::type bst
Definition: chapter-6.cpp:134
boost::mpl::end< bst >::type end_pos
Definition: chapter-6.cpp:138
Encapsulate solution for Exercise 6-0.
Encapsulate solution for Exercise 6-1.
Encapsulate solution for Exercise 6-3.
Encapsulate solution for Exercise 6-4.
A basic type to represent a binary tree.
Compels iterators to traverse the tree "in order".
Provide a type to return if the sequence is empty (no "small" type).
This is the metafunction that returns the smallest type in the sequence.
Compile-time binary to decimal number translation.
Basically indistinguishable from from any other inserter since we have a generic insert metafunction.
Perform a search on a binary tree structure.