C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
exercise-4-5.hpp
Go to the documentation of this file.
1// ===-- chapter-4/exercise-4-5.hpp ----------------------- -*- C++ -*- --=== //
9#ifndef EXERCISE_4_5
10#define EXERCISE_4_5
11
12#include <algorithm>
13
14#include <boost/mpl/if.hpp>
15#include <boost/type_traits/is_const.hpp>
16
17
42namespace exercise_4_5 {
43
61// I don't think eval_if buys you anything here, and you'd have to wrap the
62// Container iterator types with boost::mpl::identity<T> in the "return" cases.
63// The downside is that Container must have both const_iterator and iterator
64// typedefs defined.
65template <typename Container, typename Value>
66typename boost::mpl::if_<
67 boost::is_const<Container>,
68 typename Container::const_iterator,
69 typename Container::iterator>::type
70container_find(Container& c, Value const& v)
71{
72 return std::find(c.begin(), c.end(), v);
73}
74
75} // namespace exercise_4_5
76
77#endif // EXERCISE_4_5
Encapsulate solution for Exercise 4-5.
boost::mpl::if_< boost::is_const< Container >, typenameContainer::const_iterator, typenameContainer::iterator >::type container_find(Container &c, Value const &v)
Executes std::find across an entire container.