C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
exercise-6-0.hpp
Go to the documentation of this file.
1// ===-- chapter-6/exercise-6-0.hpp ----------------------- -*- C++ -*- --=== //
9#ifndef EXERCISE_6_0
10#define EXERCISE_6_0
11
12#include <boost/static_assert.hpp>
13
14#include <boost/mpl/copy.hpp>
15#include <boost/mpl/if.hpp>
16#include <boost/mpl/placeholders.hpp>
17#include <boost/mpl/vector.hpp>
18using namespace boost::mpl::placeholders;
19
20
50namespace exercise_6_0 {
51
53struct no_type { };
54
65template <typename T1, typename T2>
67 : boost::mpl::if_<boost::mpl::bool_<(sizeof(T1) < sizeof(T2))>, T1, T2> { };
68
74template <typename T2>
75struct find_smaller<no_type, T2>
76{
77 typedef T2 type;
78};
79
87template <typename State = no_type, typename Operation = find_smaller<_, _> >
88struct replace_with_smaller
89{
90 typedef Operation operation;
91 typedef State state;
92};
93
103template <typename S>
104struct smallest : boost::mpl::copy< S, replace_with_smaller<> > { };
105
106} // namespace exercise_6_0
107
108#endif // EXERCISE_6_0
Encapsulate solution for Exercise 6-0.
Find the type with a smaller size in bytes.
Provide a type to return if the sequence is empty (no "small" type).