Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2006-06-19 09:09:46


"Ovanes Markarian" <om_boost_at_[hidden]> writes:

> Hello all,
>
> somehow I have extreme difficulties to use mpl. Please do not
> understand this as any kind of blaming. I find the library really
> great and the possiblities it offers are tremendous. Currently I
> have some sort of problem, where I stuck at:
>
> I have an mpl::vector instance of form (mpl namespace was left out
> for better readability):

> class A;
> class B;
> class C;
>
> typedef vector<
> pair<int_<0>, A>,
> pair<int_<1>, B>,
> pair<int_<2>, C> > types_vector;
>
> Now I would like to use the mpl::transform algorithm to transform
> this vector into the mpl::map. I try to do it in the following way:
>
> typedef mpl::transform<type_vector, mpl::insert<mpl::map<>,
> mpl::_1 >::type type_map;
>
> And I also understand that _1 has no embedded typedefs for first and
> second, what a pair should have, so I am stuck here.

Well, transform doesn't look like the right algorithm to use in this
way. What you're doing would produce a new vector of 3 single-element
maps. You want to insert an element into a map, and then insert an
element into the result of that insertion, etc. That's the pattern of
the fold<> algorithm:

    fold<
        type_vector
      , map<>
      , insert<_,_>
>::type

copy is also probably an appropriate algorithm, but you'd need to use
an Inserter.

http://www.boost.org/libs/mpl/doc/refmanual/copy.html
http://www.boost.org/libs/mpl/doc/refmanual/inserter.html
http://www.boost.org/libs/mpl/doc/refmanual/inserters-inserter.html

    copy<
        type_vector
      , inserter< map<>, insert<_,_> >
>::type

Also, I'm afraid the MPL associative containers in 1.33.1 had some
serious bugs that could prevent you from succeeding with this. I
suggest if the above doesn't work, you first replace map<> with map0
in the examples, and then if that fails, check out the RC_1_34_0
branch from our CVS repository and try again.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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