Guest User

some template meta-hell fun

a guest
Feb 20th, 2014
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <typeinfo>
  4. #include <boost/cstdint.hpp>
  5.  
  6. #include <boost/mpl/vector.hpp>
  7. #include <boost/mpl/fold.hpp>
  8. #include <boost/mpl/map.hpp>
  9. #include <boost/mpl/pair.hpp>
  10. #include <boost/mpl/int.hpp>
  11. #include <boost/mpl/next.hpp>
  12. #include <boost/mpl/insert.hpp>
  13. #include <boost/mpl/at.hpp>
  14.  
  15. using namespace boost::mpl;
  16.  
  17. typedef vector<uint8_t, uint16_t, uint32_t, uint64_t> types_integral;
  18. typedef vector<float, double, long double> types_floating;
  19. typedef vector<std::string> types_string;
  20.  
  21. typedef vector<types_integral, types_floating, types_string> type_sets;
  22.  
  23. template<typename M1, typename M2>
  24. struct merge_maps
  25. {
  26.     typedef typename fold<M1, M2, insert<_1, _2> >::type type;
  27. };
  28.  
  29. template<typename S>
  30. struct make_type_map
  31. {
  32.     typedef typename fold<
  33.         S,
  34.         map<>,
  35.         insert<_1, pair<_2, S> > >::type type;
  36. };
  37.  
  38. typedef fold<
  39.     type_sets,
  40.     map<>,
  41.     merge_maps<_1, make_type_map<_2> > >::type map_t2s;
  42.  
  43. template<typename T>
  44. struct get_comparable
  45. {
  46.     typedef typename at<map_t2s, T>::type type;
  47. };
  48.  
  49. int main()
  50. {
  51.     std::cout << typeid(get_comparable<float>::type).name() << std::endl << std::endl;
  52.     std::cout << typeid(get_comparable<std::string>::type).name() << std::endl << std::endl;
  53.     std::cout << typeid(get_comparable<uint64_t>::type).name() << std::endl << std::endl;
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment