Guest User

Untitled

a guest
Oct 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <int N>
  4. struct placeholder
  5. {};
  6.  
  7. constexpr placeholder<1> _1;
  8. constexpr placeholder<2> _2;
  9. constexpr placeholder<3> _3;
  10.  
  11. template <typename A>
  12. struct G
  13. {
  14. G(A a)
  15. : a(a)
  16. {}
  17.  
  18. A operator()() const
  19. {
  20. return a;
  21. }
  22.  
  23. template <typename B1>
  24. A operator()(B1 b1) const
  25. {
  26. return a;
  27. }
  28.  
  29. template <typename B1, typename B2>
  30. A operator()(B1 b1, B2 b2) const
  31. {
  32. return a;
  33. }
  34.  
  35. template <typename B1, typename B2, typename B3>
  36. A operator()(B1 b1, B2 b2, B3 b3) const
  37. {
  38. return a;
  39. }
  40.  
  41. A a;
  42. };
  43.  
  44. template <>
  45. struct G<placeholder<1> >
  46. {
  47. G(placeholder<1>)
  48. {}
  49.  
  50. template <typename B1>
  51. B1 operator()(B1 b1) const
  52. {
  53. return b1;
  54. }
  55.  
  56. template <typename B1, typename B2>
  57. B1 operator()(B1 b1, B2 b2) const
  58. {
  59. return b1;
  60. }
  61.  
  62. template <typename B1, typename B2, typename B3>
  63. B1 operator()(B1 b1, B2 b2, B3 b3) const
  64. {
  65. return b1;
  66. }
  67. };
  68.  
  69. template <>
  70. struct G<placeholder<2> >
  71. {
  72. G(placeholder<2>)
  73. {}
  74.  
  75. template <typename B1, typename B2>
  76. B2 operator()(B1 b1, B2 b2) const
  77. {
  78. return b2;
  79. }
  80.  
  81. template <typename B1, typename B2, typename B3>
  82. B2 operator()(B1 b1, B2 b2, B3 b3) const
  83. {
  84. return b2;
  85. }
  86. };
  87.  
  88. template <>
  89. struct G<placeholder<3> >
  90. {
  91. G(placeholder<3>)
  92. {}
  93.  
  94. template <typename B1, typename B2, typename B3>
  95. B3 operator()(B1 b1, B2 b2, B3 b3) const
  96. {
  97. return b3;
  98. }
  99. };
  100.  
  101. template <typename F, typename A1, typename A2, typename A3>
  102. struct bind_t
  103. {
  104. bind_t(F f, A1 a1, A2 a2, A3 a3)
  105. : f(f)
  106. , g1(a1)
  107. , g2(a2)
  108. , g3(a3)
  109. {}
  110.  
  111. void operator()() const
  112. {
  113. return f(g1(), g2(), g3());
  114. }
  115.  
  116. template <typename B1>
  117. void operator()(B1 b1) const
  118. {
  119. return f(g1(b1), g2(b1), g3(b1));
  120. }
  121.  
  122. template <typename B1, typename B2>
  123. void operator()(B1 b1, B2 b2) const
  124. {
  125. return f(g1(b1, b2), g2(b1, b2), g3(b1, b2));
  126. }
  127.  
  128. template <typename B1, typename B2, typename B3>
  129. void operator()(B1 b1, B2 b2, B3 b3) const
  130. {
  131. return f(g1(b1, b2, b3), g2(b1, b2, b3), g3(b1, b2, b3));
  132. }
  133.  
  134. F f;
  135. G<A1> g1;
  136. G<A2> g2;
  137. G<A3> g3;
  138. };
  139.  
  140. template <typename F, typename A1, typename A2, typename A3>
  141. bind_t<F, A1, A2, A3> bind(F f, A1 a1, A2 a2, A3 a3)
  142. {
  143. return bind_t<F, A1, A2, A3>(f, a1, a2, a3);
  144. }
  145.  
  146.  
  147. void f(int a, int b, int c)
  148. {
  149. std::cout << a << b << c << std::endl;
  150. }
  151.  
  152. int main()
  153. {
  154. bind(&f, 1, 2, 3)();
  155. bind(&f, _1, _3, _2)(4, 6, 5);
  156. bind(&f, _1, _1, _2)(7, 8);
  157. return 0;
  158. }
Add Comment
Please, Sign In to add comment