C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
chapter-6-tree.hpp
Go to the documentation of this file.
1// ===-- chapter-6/chapter-6-tree.hpp --------------------- -*- C++ -*- --=== //
9#ifndef CHAPTER_6_TREE
10#define CHAPTER_6_TREE
11
12#include "chapter-5-tree.hpp"
13
14#include <boost/static_assert.hpp>
15
16#include <boost/mpl/greater.hpp>
17#include <boost/mpl/if.hpp>
18#include <boost/mpl/push_back.hpp>
19
20#include <boost/type_traits/is_same.hpp>
21
22
23namespace boost {
24namespace mpl {
25
28
37template<>
38struct push_back_impl<chapter5::tree_tag>
39{
42 template <typename S, typename New>
43 struct apply
44 : boost::mpl::eval_if<
45 boost::mpl::greater<S, New>,
46 chapter5::tree<S, New, void_>,
47 chapter5::tree<S, void_, New> >
48 { };
49
52 template <typename New, typename Cur, typename L, typename R>
53 struct apply<chapter5::tree<Cur, L, R>, New>
54 : boost::mpl::eval_if<
55 boost::mpl::greater<Cur, New>,
56 chapter5::tree<
57 Cur,
58 typename apply<L, New>::type,
59 R>,
60 chapter5::tree<
61 Cur,
62 L,
63 typename apply<R, New>::type>
64 >
65 { };
66
68 template <typename New>
69 struct apply<chapter5::tree<>, New>
70 {
72 };
73
76 template <typename New>
77 struct apply<void_, New>
78 {
79 typedef New type;
80 };
81};
82
84
85} // namespace mpl
86} // namespace boost
87
88
90typedef boost::mpl::push_back<t1, boost::mpl::int_<5> >::type t2;
91typedef boost::mpl::push_back<t2, boost::mpl::int_<3> >::type t3;
92typedef boost::mpl::push_back<t3, boost::mpl::int_<7> >::type t4;
93typedef boost::mpl::push_back<t4, boost::mpl::int_<1> >::type t5;
94
95typedef chapter5::tree<
96 boost::mpl::int_<5>,
97 chapter5::tree<boost::mpl::int_<3>, boost::mpl::int_<1>, void_>,
98 boost::mpl::int_<7>
100BOOST_STATIC_ASSERT((boost::is_same<t5, expected_t5>::value));
101
102#endif // CHAPTER_6_TREE
Define a binary tree structure for future exercises.
BOOST_STATIC_ASSERT((boost::is_same< t5, expected_t5 >::value))
boost::mpl::push_back< t3, boost::mpl::int_< 7 > >::type t4
chapter5::tree< boost::mpl::int_< 5 >, chapter5::tree< boost::mpl::int_< 3 >, boost::mpl::int_< 1 >, void_ >, boost::mpl::int_< 7 > > expected_t5
boost::mpl::push_back< t2, boost::mpl::int_< 3 > >::type t3
chapter5::tree t1
boost::mpl::push_back< t1, boost::mpl::int_< 5 > >::type t2
boost::mpl::push_back< t4, boost::mpl::int_< 1 > >::type t5
Exists to inject functionality into the Boost MPL namespace.
Exists to inject functionality into the Boost namespace.
Provide utilities availble to all Chapter 5 solutions.
A basic type to represent a binary tree.