fork download
  1. #include <utility>
  2. #include <functional>
  3. #include <iostream>
  4.  
  5. template<class Is, size_t I>
  6. struct add;
  7. template<class Is, size_t I>
  8. using add_t=typename add<Is,I>::type;
  9.  
  10. template<size_t...Is, size_t I>
  11. struct add<std::index_sequence<Is...>, I>{
  12. using type=std::index_sequence<(I+Is)...>;
  13. };
  14.  
  15. template<template<class...>class Z, class Is, class...Ts>
  16. struct partial_apply;
  17. template<template<class...>class Z, class Is, class...Ts>
  18. using partial_apply_t=typename partial_apply<Z,Is,Ts...>::type;
  19.  
  20. template<template<class...>class Z, size_t...Is, class...Ts>
  21. struct partial_apply<Z,std::index_sequence<Is...>, Ts...> {
  22. using tup = std::tuple<Ts...>;
  23. template<size_t I> using e = std::tuple_element_t<I, tup>;
  24.  
  25. using type=Z< e<Is>... >;
  26. };
  27.  
  28. template<template<class...>class Z, class...Ts>
  29. struct split {
  30. using left = partial_apply_t<Z, std::make_index_sequence<sizeof...(Ts)/2>, Ts...>;
  31. using right = partial_apply_t<Z, add_t<
  32. std::make_index_sequence<(1+sizeof...(Ts))/2>,
  33. sizeof...(Ts)/2
  34. >, Ts...>;
  35. };
  36. template<template<class...>class Z, class...Ts>
  37. using right=typename split<Z,Ts...>::right;
  38. template<template<class...>class Z, class...Ts>
  39. using left=typename split<Z,Ts...>::left;
  40.  
  41. template<class...Sigs>
  42. struct functions_impl;
  43.  
  44. template<class...Sigs>
  45. using functions = typename functions_impl<Sigs...>::type;
  46.  
  47. template<class...Sigs>
  48. struct functions_impl:
  49. left<functions, Sigs...>,
  50. right<functions, Sigs...>
  51. {
  52. using type=functions_impl;
  53. using A = left<functions, Sigs...>;
  54. using B = right<functions, Sigs...>;
  55. using A::operator();
  56. using B::operator();
  57. template<class F>
  58. functions_impl(F&& f):
  59. A(f),
  60. B(std::forward<F>(f))
  61. {}
  62. };
  63. template<class Sig>
  64. struct functions_impl<Sig> {
  65. using type=std::function<Sig>;
  66. };
  67.  
  68. int main()
  69. {
  70. functions<void(int), void(double)> f = [](auto&& x){std::cout << x << '\n';};
  71. f(3.14);
  72. f(3);
  73. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
3.14
3