Boost logo

Boost Users :

Subject: Re: [Boost-users] mpl::for_each state
From: Skelly, Luke (skelly_at_[hidden])
Date: 2009-04-01 10:59:34


> Have MapMaker store a reference to map. (Warning untested code)
>
> typedef std::map<std::size_t, std::size_t> map_type;
>
> struct MapMaker {
> map_type& m;
> template<class T>
> void operator()(T) const {
> func_map[U::first::value]=U::second::value;
> }
> };
>
> template<class MplMap>
> map_type makeMap() {
> map_type result;
> MapMaker f = { result };
> boost::mpl::for_each<MplMap>(f);
> return result;
> }

Yes, since I wasn't able to send a reference directly, you're suggesting including a reference within the object being passed to mpl::for_each. This is a nice straightforward solution and in some ways cleaner.

I was looking for a way of sending the reference directly. I think I've found it by using boost::ref.

typedef std::map<std::size_t, std::size_t> map_type;

struct MapMaker {
    map_type m;
    template<class T>
    void operator()(T) const {
        m[U::first::value]=U::second::value;
    }
};

template<class MplMap>
map_type makeMap() {
    MapMaker f;
    boost::mpl::for_each<MplMap>(boost::ref(f));
    return f.m;
}

Luke Skelly


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