Boost logo

Boost Users :

From: Martin Pasdzierny (martin_at_[hidden])
Date: 2005-01-31 15:04:53


Hello MPL experts !
Stepping forward in dealing with a mpl::vector_c of enums I found it hard to determine the correct expression to 'mpl::find' an element within such a vector.
The code below compiles with gcc 3.3.3 and shows 5 different approaches, but only version 4,5 really find the correct iterator.
Version 1,2,3 return iterators pointing to the vectors end.
It would be helpful if someone more familiar with MPL's concepts could give some explanation, particularly about the aspects:

- Is there a solution using mpl::find instead of mpl::find_if
   If not, why the difference between finding elements in mpl::vector and mpl::vector_c ?
- What is the difference between using integral wrappers:
     boost::mpl::integral_c<MY_ENUM,c> >
     boost::mpl::int_<c>
- And still the question from the previous posting:
   Can I (should I) tell mpl::vector_c I'm using my own enum type and not plain int ?

regards, Martin.

<CODE>
#include <iostream>

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/end.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/integral_c.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/distance.hpp>
#include <boost/mpl/placeholders.hpp>

using namespace boost::mpl::placeholders;

enum MY_ENUM {X=12,Y=24,Z=36};

typedef boost::mpl::vector_c<int,X,Y> my_vector_c;

template<typename MyEnums,MY_ENUM c> struct test {
   static const MY_ENUM value = c;
   typedef typename boost::mpl::find<MyEnums,boost::mpl::int_<c> >::type c_iter_1;
   typedef typename boost::mpl::find<MyEnums,boost::mpl::integral_c<MY_ENUM,c> >::type c_iter_2;
   typedef typename boost::mpl::find<MyEnums,boost::mpl::integral_c<MY_ENUM,c> >::type c_iter_3;
   typedef typename boost::mpl::find_if<MyEnums,boost::mpl::equal_to<_1,boost::mpl::int_<c> > >::type c_iter_4;
   typedef typename boost::mpl::find_if<MyEnums,boost::mpl::equal_to<_1,boost::mpl::integral_c<MY_ENUM,c> > >::type c_iter_5;
// these 3 lines are only for showing the end iterators position:
   typedef typename boost::mpl::end<MyEnums>::type end_iter;
   typedef typename end_iter::pos end_pos_t;
   static const int end_pos = end_pos_t::value;
};

int main(int argc, char** argv) {
   std::cout << "\ntest<.,X>::end_pos:" << test<my_vector_c,X>::end_pos << std::endl;
   std::cout << "\ntest<.,Y>::c_iter_1::pos::value:" << test<my_vector_c,Y>::c_iter_1::pos::value << std::endl;
   std::cout << "\ntest<.,Y>::c_iter_2::pos::value:" << test<my_vector_c,Y>::c_iter_2::pos::value << std::endl;
   std::cout << "\ntest<.,Y>::c_iter_3::pos::value:" << test<my_vector_c,Y>::c_iter_3::pos::value << std::endl;
   std::cout << "\ntest<.,Y>::c_iter_4::pos::value:" << test<my_vector_c,Y>::c_iter_4::pos::value << std::endl;
   std::cout << "\ntest<.,Y>::c_iter_5::pos::value:" << test<my_vector_c,Y>::c_iter_5::pos::value << std::endl;
}

</CODE>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net