#include #include #include #include #include #include #include #include #include #include namespace mpl = boost::mpl; //============================================================================= //Template meta function that joins two maps (normally in a header file) template< class, class MapL, class MapR > struct join_maps_imp; //============================================================================= template< class MapL, class MapR > struct join_maps { typedef typename join_maps_imp< typename mpl::less< typename mpl::size::type , typename mpl::size::type >::type , MapL , MapR >::type type; }; //============================================================================= //MapR is smaller than MapL, so add R to L template< class MapL, class MapR > struct join_maps_imp { typedef typename join_maps_imp::type type; }; //============================================================================= namespace join_maps_detail { //The loop template struct next_elem { typedef typename mpl::deref< iter >::type MapElem; typedef typename mpl::insert< OldMap, MapElem >::type NewMap; typedef typename mpl::next::type next_iter; typedef typename next_elem::value , next_iter , end_iter , NewMap >::type type; }; //Loop termination template struct next_elem { typedef NewMap type; }; }; //============================================================================= //MapL is smaller than MapR, so add L to R template< class MapL, class MapR > struct join_maps_imp { //Initialize the loop typedef typename mpl::begin::type iter; typedef typename mpl::end::type end_iter; typedef typename join_maps_detail::next_elem::type type; }; //============================================================================= template struct print {}; //============================================================================= template std::ostream& operator<<(std::ostream& out, const print&) { out << i; return out; } //============================================================================= struct map_printer { template inline void operator() (T) const { std::cout << " " << typename T::second(); } }; /*###################################################################*/ int main(int argc, const char* argv[]) { //Create a couple of maps using insert (this replicates the actual code) typedef mpl::insert, mpl::pair< mpl::int_<0>, print<0> > >::type map0; typedef mpl::insert, print<1> > >::type map1; typedef mpl::insert, print<2> > >::type map2; typedef mpl::insert, mpl::pair< mpl::int_<3>, print<3> > >::type map3; typedef mpl::insert, print<4> > >::type map4; typedef mpl::insert, print<5> > >::type map5; //Erease a couple of keys typedef mpl::erase_key >::type map_erase1; typedef mpl::erase_key >::type map_erase2; //Join the maps into a single map typedef join_maps< map2 , map_erase2 >::type join1; typedef join_maps< map_erase1, map5 >::type join2; typedef join_maps< map_erase1, map_erase2 >::type join3; //Print out the resuling maps std::cout << std::endl; boost::mpl::for_each< join1 >(map_printer()); //Works std::cout << std::endl; boost::mpl::for_each< join2 >(map_printer()); //Works std::cout << std::endl; boost::mpl::for_each< join3 >(map_printer()); //MSVC 9 error (works with CYGWIN GCC 4.3.2 ) std::cout << std::endl; }