[Home]CPPTM Answers - Exercise 4-5

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

Difference (from prior major revision) (no other diffs)

Changed: 61c61,63
-mahall
-mahall

I used mpl::eval_if because it was/is the 'default' condition testing metafunction for me. I agree that mpl::if_ is more appropriate for this case. - Ariel

 // (by Ariel Badichi)
 #include <vector>
 #include <algorithm>
 #include <cassert>

 #include <boost/assign.hpp>
 #include <boost/type_traits/is_const.hpp>
 #include <boost/mpl/eval_if.hpp>
 #include <boost/mpl/identity.hpp>

 namespace mpl = boost::mpl;

 template<typename C>
 struct iterator_type : mpl::eval_if<
                            boost::is_const<C>,
                            mpl::identity<typename C::const_iterator>,
                            mpl::identity<typename C::iterator>
                        >
 {};

 template<typename C, typename V>
 typename iterator_type<C>::type find(C & c, const V & v)
 {
     return std::find(c.begin(), c.end(), v);
 }

 int main()
 {
     using namespace boost::assign;

     std::vector<int> v1;
     v1 += 1, 2, 30, 40, 500;

     assert(find(v1, 40) != v1.end());
     assert(find(v1, 41) == v1.end());

     const std::vector<int> v2(v1);

     assert(find(v2, 40) != v2.end());
     assert(find(v2, 41) == v2.end());

     return 0;
 }


Do you gain anything by using mpl::eval_if here, instead of just plain mpl::if_? In the following:

 template <typename Container>
 struct get_iterator_type : mpl::if_<
   boost::is_const<Container>,
   typename Container::const_iterator,
   typename Container::iterator>
 {};

neither the true nor false branch of the if_ statement are metafunctions, so using eval_if doesn't seem to defer any metaffunction calls.

-mahall

I used mpl::eval_if because it was/is the 'default' condition testing metafunction for me. I agree that mpl::if_ is more appropriate for this case. - Ariel


BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited April 10, 2005 3:26 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers