C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
chapter-3.cpp
Go to the documentation of this file.
1// ===-- chapter-3/chapter-3.cpp -------------------------- -*- C++ -*- --=== //
20#if __cplusplus >= 202002L
21# include "c++20/exercise-3-0.hpp"
22#else
23# include "exercise-3-0.hpp"
24#endif
25#include "exercise-3-1.hpp"
26#include "exercise-3-2.hpp"
27#include "exercise-3-3.hpp"
28#include "exercise-3-4.hpp"
29#include "exercise-3-5.hpp"
30#include "exercise-3-6.hpp"
31
32#include <boost/static_assert.hpp>
33
34#include <boost/mpl/equal.hpp>
35#include <boost/mpl/vector_c.hpp>
36
37
40namespace test_3_0
41{
42using namespace exercise_3_0;
43
44#if __cplusplus >= 202002L
45constexpr uint32_t one = checked_binary(1);
46constexpr uint32_t three = checked_binary(11);
47constexpr uint32_t five = checked_binary(101);
48constexpr uint32_t seven = checked_binary(111);
49constexpr uint32_t nine = checked_binary(1001);
50
51# ifdef NEGATIVE_TEST // Fails "check" in checked_binary().
52constexpr uint32_t bogus = checked_binary(2);
53# endif
54#else
60
61# ifdef NEGATIVE_TEST // Fails BOOST_STATIC_ASSERT.
62unsigned const bogus = checked_binary<2>::value;
63# endif
64#endif
65}
66
67
70namespace test_3_1 {
71using namespace exercise_3_1;
72
73typedef boost::mpl::vector_c<int, 1, 2, 3> init_vector_t;
74
78
81
83 boost::mpl::equal<
85 boost::mpl::vector_c<int, 2, 3, 4> >::type::value));
86
87} // namespace test_3_1
88
89
92namespace test_3_2 {
93using namespace exercise_3_2;
94
95typedef boost::mpl::vector_c<int, 1, 2, 3> init_vector_t;
96
99
101 boost::mpl::equal<
103 boost::mpl::vector_c<int, 1, 4, 9> >::type::value));
104
105} // namespace test_3_2
106
107
108namespace {
109
112static void test_exercise_3_5()
113{
114 using namespace exercise_3_5::section_3_1;
115
116 // Test simple addition
117 {
118 quantity<float, length> len1(1.0);
119 quantity<float, length> const len2(2.0);
120
121 len1 = len1 + len2;
122 }
123
124 // Test multiplication
125 {
126 quantity<float, mass> const m(2.3);
128
129 quantity<float, force> const f = m*a;
130 f.value(); // Quiet the unused var warning w/o disabling it totally
131 }
132
133 // Test division
134 {
135 quantity<float, length> const l(1.0);
136 quantity<float, time> const s(3.0);
137
138 quantity<float, velocity> const v = l/s;
139 v.value(); // Quiet the unused var warning w/o disabling it totally
140 }
141
142 // Exercise 3-5
143 {
145 quantity<float, mass> const m(2.3);
147
148 f = f + m * a;
149 }
150}
151
152} // namespace <anon>
153
172int main()
173{
175 return 0;
176}
Solution to Exercise 3-0 (C++20).
int main()
Definition: chapter-3.cpp:172
A numerical quanitity with intrinsic dimensions.
T value() const
Return the current quantity's value.
Solution to Exercise 3-0.
Solution to Exercise 3-1.
Solution to Exercise 3-2.
Solution to Exercise 3-3.
Solution to Exercise 3-4.
Solution to Exercise 3-5.
Solution to Exercise 3-6.
static void test_exercise_3_5()
Tests for Exercise 3-5.
Definition: chapter-3.cpp:112
Encapsulate solution for Exercise 3-0.
Encapsulate solution for Exercise 3-1.
Encapsulate solution for Exercise 3-2.
Modification of algorithms from Chapter 3, Section 1.
Tests functionality from Exercise 3-0.
Definition: chapter-3.cpp:41
unsigned const three
Definition: chapter-3.cpp:56
unsigned const nine
Definition: chapter-3.cpp:59
unsigned const five
Definition: chapter-3.cpp:57
unsigned const one
Definition: chapter-3.cpp:55
unsigned const seven
Definition: chapter-3.cpp:58
Tests functionality from Exercise 3-1.
Definition: chapter-3.cpp:70
BOOST_STATIC_ASSERT((boost::mpl::equal< new_vectorb_t, boost::mpl::vector_c< int, 2, 3, 4 > >::type::value))
new_vectorb_t z
Definition: chapter-3.cpp:80
ex_3_1b< init_vector_t >::type new_vectorb_t
Definition: chapter-3.cpp:79
ex_3_1< init_vector_t >::type new_vector_t
Definition: chapter-3.cpp:75
new_vector_t y
Definition: chapter-3.cpp:77
init_vector_t x
Definition: chapter-3.cpp:76
boost::mpl::vector_c< int, 1, 2, 3 > init_vector_t
Definition: chapter-3.cpp:73
Tests functionality from Exercise 3-2.
Definition: chapter-3.cpp:92
BOOST_STATIC_ASSERT((boost::mpl::equal< new_vector_t, boost::mpl::vector_c< int, 1, 4, 9 > >::type::value))
ex_3_2< init_vector_t >::type new_vector_t
Definition: chapter-3.cpp:97
new_vector_t x
Definition: chapter-3.cpp:98
boost::mpl::vector_c< int, 1, 2, 3 > init_vector_t
Definition: chapter-3.cpp:95
Compile-time binary to decimal number translation.
Add 1 to all items in a sequence (w/o placeholder args).
Add 1 to all items in a sequence (w/ placeholder args).
Given a sequence of numbers, return a sequence of their squares.