fork download
  1. #include <iostream>
  2. #include <tuple>
  3.  
  4. template<class T, class F>
  5. class predicate {
  6. public:
  7. explicit predicate(T const& t, F const& p = F())
  8. : value(t)
  9. , pred(f) {}
  10.  
  11. bool match(T const& value2) {
  12. return pred(value, value2);
  13. }
  14.  
  15. private:
  16. T value;
  17. F pred;
  18. };
  19.  
  20. template<class T>
  21. struct __op_eq {
  22. operator()(T const& a, T const& b) {
  23. return a == b;
  24. }
  25. };
  26.  
  27. template<class T>
  28. struct __op_gt {
  29. operator()(T const& a, T const& b) {
  30. return a > b;
  31. }
  32. };
  33.  
  34. template<class T> using eq_pred = predicate<T, op_eq<T>>;
  35. template<class T> using gt_pred = predicate<T, op_gt<T>>;
  36.  
  37. template<class... Ts>
  38. class __match {
  39. public:
  40. template<class... Us>
  41. __match(Us&&... argz) : args(argz...) {}
  42.  
  43. template<class U, class Us...>
  44. match& operator=(std::tuple<U, Us...> t) {
  45. do_match<0>(t);
  46. do_assign<0>(t);
  47. return *this;
  48. }
  49.  
  50. private:
  51. void do_match(std::tuple<> const&) {}
  52.  
  53. template<std::size_t I, class U, class... Us>
  54. void do_match(std::tuple<U, Us...> const& tup) {
  55. do_match<I + 1>(std::tail(tup));
  56. }
  57.  
  58. template<std::size_t I, class U, class... Us, class V, class W>
  59. void do_match(std::tuple<predicate<V, W>, Us...> const& tup) {
  60. if (!std::get<0>(tup).match(std::get<I>(args))) {
  61. throw match_error{};
  62. }
  63.  
  64. do_match<I + 1>(std::tail(tup));
  65. }
  66.  
  67. void do_assign(std::tuple<> const&) {}
  68.  
  69. template<std::size_t I, class U, class... Us>
  70. void do_assign(std::tuple<U, Us...> tup) {
  71. std::get<0>(tup) = std::get<I>(args);
  72. do_assign<I + 1>(std::tail(tup));
  73. }
  74.  
  75. template<std::size_t I, class U, class... Us, class V, class W>
  76. void do_assign(std::tuple<predicate<V, W>, Us...> const& tup) {
  77. do_assign<I + 1>(std::tail(tup));
  78. }
  79.  
  80. std::tuple<Ts&...> args;
  81. };
  82.  
  83. template<class... Ts>
  84. __match<Ts...> match(Ts&&... ts) {
  85. return __match<Ts...>{ts...};
  86. }
  87.  
  88. int main() {
  89. int a, b, c;
  90. match(a, b, eq_pred{42}, c) = std::make_tuple(1, 2, 42, 3);
  91. std::cout << a << ", " << b << ", " << c << '\n';
  92.  
  93. match(a, b, eq_pred{43}, c) = std::make_tuple(1, 2, 42, 3);
  94. std::cout << a << ", " << b << ", " << c << '\n';
  95. }
  96.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In constructor 'predicate<T, F>::predicate(const T&, const F&)':
prog.cpp:9:12: error: 'f' was not declared in this scope
prog.cpp: At global scope:
prog.cpp:22:38: error: ISO C++ forbids declaration of 'operator()' with no type
prog.cpp:29:38: error: ISO C++ forbids declaration of 'operator()' with no type
prog.cpp:34:19: error: expected unqualified-id before 'using'
prog.cpp:35:19: error: expected unqualified-id before 'using'
prog.cpp:43:31: error: 'struct Us' is not a valid type for a template constant parameter
prog.cpp:44:5: error: 'match' does not name a type
prog.cpp:50:1: error: expected unqualified-id before 'private'
prog.cpp: In member function 'void __match<Ts>::do_match(const std::tuple<U, Us ...>&)':
prog.cpp:55:25: error: 'tail' is not a member of 'std'
prog.cpp: In member function 'void __match<Ts>::do_match(const std::tuple<predicate<V, W>, Us ...>&)':
prog.cpp:61:19: error: 'match_error' was not declared in this scope
prog.cpp:61:30: error: expected ';' before '{' token
prog.cpp:64:25: error: 'tail' is not a member of 'std'
prog.cpp: In member function 'void __match<Ts>::do_assign(std::tuple<U, Us ...>)':
prog.cpp:72:26: error: 'tail' is not a member of 'std'
prog.cpp: In member function 'void __match<Ts>::do_assign(const std::tuple<predicate<V, W>, Us ...>&)':
prog.cpp:77:26: error: 'tail' is not a member of 'std'
prog.cpp: In function 'int main()':
prog.cpp:90:17: error: 'eq_pred' was not declared in this scope
prog.cpp:93:24: error: expected ')' before '{' token
stdout
Standard output is empty