Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. // compile: clang++ -Wall -std=c++11 -I /usr/local/include mp11.cpp -o mp11
  2.  
  3. #include <vector>
  4. #include <iostream>
  5. #include <stdexcept>
  6.  
  7. #include <boost/mp11/list.hpp>
  8. #include <boost/mp11/integral.hpp>
  9. #include <boost/mp11/algorithm.hpp>
  10.  
  11. namespace mp11 = boost::mp11;
  12.  
  13. using num_types = mp11::mp_list<int, long, float, double>;
  14.  
  15. template <typename T, std::size_t N> struct dispatch_impl {
  16. template <std::size_t K, template<typename> class Func, typename ...Ar>
  17. static auto call(size_t i, Ar&&... rg) ->
  18. decltype(Func< mp11::mp_at_c<T, 0> >()(std::forward<Ar>(rg)...)) {
  19. if (i == 0) {
  20. std::cout << "hello" << "\n";
  21. return Func< mp11::mp_at_c<T, K> >()(std::forward<Ar>(rg)...);
  22. } else {
  23. return dispatch_impl<T, N - 1>::template call<K + 1, Func>(i - 1,
  24. std::forward<Ar>(rg)...);
  25. }
  26. }
  27. };
  28.  
  29. template <typename T> struct dispatch_impl<T, 0> { };
  30.  
  31. template <typename T> struct dispatch_impl<T, 1> {
  32. template <std::size_t K, template<typename> class Func, typename ...Ar>
  33. static auto call(size_t i, Ar&&... rg) ->
  34. decltype(Func< mp11::mp_at_c<T, 0> >()(std::forward<Ar>(rg)...)) {
  35. if (i == 0) {
  36. return Func< mp11::mp_at_c<T, K> >()(std::forward<Ar>(rg)...);
  37. } else {
  38. throw std::runtime_error("error");
  39. }
  40. }
  41. };
  42.  
  43. template <template<typename> class Func, typename ...Ar>
  44. auto dispatch_type(size_t type, Ar&&... rg) ->
  45. decltype(Func< mp11::mp_at_c<num_types, 0> >()(std::forward<Ar>(rg)...)) {
  46. using N = mp11::mp_size<num_types>;
  47. return dispatch_impl< num_types, std::size_t{N::value} >::template call<0,
  48. Func>(type, std::forward<Ar>(rg)...);
  49. }
  50.  
  51. template <class T>
  52. struct Length {
  53. size_t operator()(size_t len) {
  54. std::vector<T> vec(len, 100);
  55. return vec.size();
  56. }
  57. };
  58. template <>
  59. struct Length <int> {
  60. size_t operator()(size_t len) {
  61. std::vector<int> vec(len + 1, 100);
  62. return vec.size();
  63. }
  64. };
  65. template <>
  66. struct Length <float> {
  67. size_t operator()(size_t len) {
  68. std::vector<float> vec(len + 2, 100);
  69. return vec.size();
  70. }
  71. };
  72. template <>
  73. struct Length <double> {
  74. size_t operator()(size_t len) {
  75. std::vector<double> vec(len + 3, 100);
  76. return vec.size();
  77. }
  78. };
  79.  
  80. size_t length(size_t type, size_t len) {
  81. return dispatch_type<Length>(type, len);
  82. }
  83.  
  84. int main () {
  85.  
  86. std::cout << "length: " << length(0, 0) << "\n";
  87. std::cout << "length: " << length(1, 0) << "\n";
  88. std::cout << "length: " << length(2, 0) << "\n";
  89. std::cout << "length: " << length(3, 0) << "\n";
  90. std::cout << "length: " << length(4, 0) << "\n";
  91. std::cout << "length: " << length(5, 0) << "\n";
  92.  
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement