mpl::for_each state

Please ignore my last message. I somehow accidentally sent it while still editing! I am trying to create a std::map from an mpl::map. I've been trying to figure out how to use mpl::for_each to accomplish this. The problem I have is that the runtime function is passed to for_each by value and returns void. All the examples I've seen for mpl::for_each involve printing and not creating any new objects. Here is the attempt I've made: // creates std::map<size_t,size_t> from mpl::map<size_t,size_t> template<typename TypeMap> struct MapMaker { typedef std::map<size_t, size_t> map_type; map_type func_map; MapMaker() { boost::mpl::for_each<TypeMap>(*this); } template<typename U> void operator()(U x) { func_map[U::first::value]=U::second::value; } func_type operator[](size_t i) { return func_map[i]; } }; typedef boost::mpl::map< boost::mpl::pair<boost::mpl::size_t<1>, boost::mpl::size_t<2> >, boost::mpl::pair<boost::mpl::size_t<2>, boost::mpl::size_t<24> >,
myMap;
int main() { MapMaker<myMap> m; size_t result = m[1]; } Of course this is a simplified version of what I'm really trying to do, which is create a map to function pointers. The problem is in the call to mpl::for_each in the MapMaker constructor. The std::map is created, but never returned. I've considered making func_map static, but that complicates things quite a bit, especially since MapMaker it is a class template. Luke Skelly

AMDG Skelly, Luke wrote:
Please ignore my last message. I somehow accidentally sent it while still editing!
I am trying to create a std::map from an mpl::map. I've been trying to figure out how to use mpl::for_each to accomplish this. The problem I have is that the runtime function is passed to for_each by value and returns void. All the examples I've seen for mpl::for_each involve printing and not creating any new objects. Here is the attempt I've made:
<snip>
The problem is in the call to mpl::for_each in the MapMaker constructor. The std::map is created, but never returned. I've considered making func_map static, but that complicates things quite a bit, especially since MapMaker it is a class template.
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; } In Christ, Steven Watanabe

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
participants (2)
-
Skelly, Luke
-
Steven Watanabe