#include #include #include #include #include #include #include #include #include namespace te = boost::type_erasure; namespace fusion = boost::fusion; namespace mpl = boost::mpl; template struct at_key { static Reference apply(Sequence& sequence) { return fusion::at_key(sequence); } }; template struct any_map { typedef te::any< mpl::vector< te::relaxed_match, te::copy_constructible<>, typename mpl::transform< Properties, at_key< te::_self, mpl::first, mpl::second > >::type >, typename mpl::if_::type > type; }; struct key1 {}; // body needs to be defined for Boost.TypeErasure struct key2 {}; typedef fusion::map< fusion::pair, fusion::pair > map2; typedef any_map< mpl::vector< mpl::pair, mpl::pair > >::type any_map2; typedef any_map< mpl::vector< mpl::pair > >::type any_map1; typedef any_map< mpl::vector< mpl::pair >, mpl::true_ >::type any_map1_ref; int main() { const int N = 10000000; const int repeats = 100; std::vector map2_vec(N); std::vector any_map2_vec(N); for(int i = 0; i < N; ++i) { fusion::at_key(map2_vec[i]) = std::rand() % 100; any_map2_vec[i] = map2_vec[i]; } { boost::progress_timer t; int sum = 0; for(int i = 0; i < repeats; ++i) { BOOST_FOREACH(map2& x, map2_vec) { sum += fusion::at_key(x); } } std::cout << "map2: " << sum << std::endl; } { boost::progress_timer t; int sum = 0; for(int i = 0; i < repeats; ++i) { BOOST_FOREACH(any_map2& x, any_map2_vec) { sum += te::call(at_key(), x); } } std::cout << "any_map2: " << sum << std::endl; } { boost::progress_timer t; int sum = 0; for(int i = 0; i < repeats; ++i) { BOOST_FOREACH(any_map2& x, any_map2_vec) { any_map1 y(x); sum += te::call(at_key(), y); } } std::cout << "any_map1: " << sum << std::endl; } { boost::progress_timer t; int sum = 0; for(int i = 0; i < repeats; ++i) { BOOST_FOREACH(any_map2& x, any_map2_vec) { any_map1_ref y(x); sum += te::call(at_key(), y); } } std::cout << "any_map1_ref: " << sum << std::endl; } return 0; }