Boost logo

Boost Users :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2007-06-05 05:42:03


Yuval Ronen writes:
> I have the following code:
>
> #include <utility>
> #include <boost/mpl/map.hpp>
> #include <boost/mpl/transform.hpp>
>
> template <typename MplPair>
> struct make_ptr_pair1
> {
> typedef std::pair<typename MplPair::first *, typename MplPair::second *> type;
               ^^^
               mpl

> };
>
> template <typename First, typename Second>
> struct make_ptr_pair2
> {
> typedef std::pair<First *, Second *> type;
> };
>
> int main(int argc, char *argv[])
> {
> namespace mpl = boost::mpl;
> typedef mpl::map< mpl::pair<int, double> > my_map;
> //typedef mpl::transform< my_map, make_ptr_pair1<mpl::_1> >::type ptr_pair_seq1;
> //typedef mpl::transform< my_map, make_ptr_pair2<mpl::_1, mpl::_2> >::type ptr_pair_seq2;
> return 0;
> }
>
> If I uncomment any of the two mpl::transform lines in main(), the code
> doesn't compile (VC8). The point is to get a sequence of std::pairs out
> of the content of the mpl::map. Is there anything I'm doing wrong? Can
> mpl::transform be used for mpl::map?

It can be, but you need to specify the inserter manually, because the
default (front_inserter, as per
http://boost.org/libs/mpl/doc/refmanual/reversible-algorithm.html)
is not suitable for accociated sequences:

...
#include <boost/mpl/inserter.hpp>
#include <boost/mpl/insert.hpp>

...

int main(int argc, char *argv[])
{
     typedef mpl::map< mpl::pair<int, double> > my_map;
     typedef mpl::inserter< mpl::map0<>, mpl::insert<mpl::_1,mpl::_2> > map_inserter;
     typedef mpl::transform< my_map, make_ptr_pair1<mpl::_1>, map_inserter >::type ptr_pair_seq1;
     // ^^^^^^^^^^^^^^
     return 0;
}

> Maybe I can do it with fusion, but the above code seems to be
> something that's supposed to be working, isn't it?

Yes, ideally, your original code should just work. It's a design
bug. I've added the issue to the library's TODO list
(http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?MPL_TODO_List).

Thanks for the report,

-- 
Aleksey Gurtovoy
MetaCommunications Engineering

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