C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
chapter-4.cpp
Go to the documentation of this file.
1// ===-- chapter-4/chapter-4.cpp -------------------------- -*- C++ -*- --=== //
20#include "exercise-4-0.hpp"
21#include "exercise-4-1.hpp"
22#include "exercise-4-2.hpp"
23#include "exercise-4-3.hpp"
24#include "exercise-4-4.hpp"
25#include "exercise-4-5.hpp"
26
27#include <stdio.h>
28#include <vector>
29
30
33namespace test_4_1 {
34using namespace exercise_4_1;
35
36// Test logical_or...
43
44// Test logical_and...
51
52} // namespace test_4_1
53
54
57namespace test_4_2 {
58using namespace exercise_4_2;
59
60
65 >::value));
68 boost::mpl::false_,
69 boost::mpl::true_
70 >::value));
73 boost::mpl::false_,
74 boost::mpl::false_
75 >::value));
78 boost::mpl::false_,
79 boost::mpl::false_,
80 boost::mpl::false_,
81 boost::mpl::false_,
82 boost::mpl::false_
83 >::value));
86 boost::mpl::false_,
87 boost::mpl::false_,
88 boost::mpl::false_,
89 boost::mpl::false_,
90 boost::mpl::true_
91 >::value));
99 >::value));
100
101} // namespace test_4_2
102
103
106namespace test_4_3 {
107using namespace exercise_4_3;
108
109// Algorithm 1:
110typedef next_if< mpl::int_<1>, mpl::greater<mpl::int_<0>, _1> > shouldBe1;
111typedef next_if< mpl::int_<1>, mpl::greater<mpl::int_<3>, _1> > shouldBe2;
112
113BOOST_STATIC_ASSERT((shouldBe1::value == 1));
114BOOST_STATIC_ASSERT((shouldBe2::value == 2));
115
116// Algorithm 2:
117typedef formula< mpl::int_<2>, mpl::int_<2> > shouldBe6;
118typedef formula< mpl::int_<3>, mpl::int_<4> > shouldBe3;
119typedef formula< mpl::int_<9>, mpl::int_<2> > shouldBe7;
120BOOST_STATIC_ASSERT((shouldBe6::value == 6));
121BOOST_STATIC_ASSERT((shouldBe3::value == 3));
122BOOST_STATIC_ASSERT((shouldBe7::value == 7));
123
124} // namespace test_4_3
125
126
129namespace test_4_4 {
130using namespace exercise_4_4;
131
132class test_class { };
133
136
144
152
156
157} // namespace test_4_4
158
159
160namespace {
161
164static void test_exercise_4_5()
165{
166#ifdef EXERCISE_4_5
167 using namespace exercise_4_5;
168
169 static int const values[] = {1, 2, 3, 4};
170 std::vector<int> v(values, values + (sizeof(values) / sizeof(*values)));
171
172 std::vector<int>::iterator const it = container_find(v, 2);
173
174 std::vector<int> const& v_ref = v;
175 std::vector<int>::const_iterator const it_const = container_find(v_ref, 2);
176 printf("it %s it_const\n", (*it == *it_const) ? "==" : "!=");
177#endif
178}
179
180} // namespace <anon>
181
182
183int main()
184{
186 return 0;
187}
int main()
Definition: chapter-4.cpp:183
Solution to Exercise 4-0.
Solution to Exercise 4-1.
Solution to Exercise 4-2.
Solution to Exercise 4-3.
Solution to Exercise 4-4.
Solution to Exercise 4-5.
static void test_exercise_4_5()
Tests for Exercise 4-5.
Definition: chapter-4.cpp:164
Encapsulate solution for Exercise 4-1.
Encapsulate solution for Exercise 4-2.
Encapsulate solution for Exercise 4-3.
Encapsulate solution for Exercise 4-4.
Encapsulate solution for Exercise 4-5.
Tests functionality from Exercise 4-1.
Definition: chapter-4.cpp:33
BOOST_STATIC_ASSERT((logical_or< undefined_false< true >, undefined_false< false > >::value))
Tests functionality from Exercise 4-2.
Definition: chapter-4.cpp:57
BOOST_STATIC_ASSERT((logical_or5< undefined_false< true >, undefined_false< false > >::value))
Tests functionality from Exercise 4-3.
Definition: chapter-4.cpp:106
formula< mpl::int_< 9 >, mpl::int_< 2 > > shouldBe7
Definition: chapter-4.cpp:119
formula< mpl::int_< 3 >, mpl::int_< 4 > > shouldBe3
Definition: chapter-4.cpp:118
next_if< mpl::int_< 1 >, mpl::greater< mpl::int_< 3 >, _1 > > shouldBe2
Definition: chapter-4.cpp:111
BOOST_STATIC_ASSERT((shouldBe1::value==1))
next_if< mpl::int_< 1 >, mpl::greater< mpl::int_< 0 >, _1 > > shouldBe1
Definition: chapter-4.cpp:110
formula< mpl::int_< 2 >, mpl::int_< 2 > > shouldBe6
Definition: chapter-4.cpp:117
Tests functionality from Exercise 4-4.
Definition: chapter-4.cpp:129
BOOST_STATIC_ASSERT((is_data_member_pointer< int *(test_class::*)>::value))
If the template parameter is false, template instantiation is an error.
If the template parameter is true, template instantiation is an error.
Short-circuit logical OR evaluation of two boolean values.
Short-circuit logical OR evaluation of two boolean values.
Short-circuit logical OR evaluation for up to five boolean values.
Fix algorithm 2 in Exercise 4-3:
Fix algorithm 1 in Exercise 4-3:
Returns true if T is a pointer, but not a member function pointer.
Returns true if T is a pointer to a (non-member) function type.
Returns true if T is a reference to a pointer to a (non-member) function!
Returns true if T is a reference type to a constant type.