9
3

bind.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef BOOST_MP11_BIND_HPP_INCLUDED
  2. #define BOOST_MP11_BIND_HPP_INCLUDED
  3. // Copyright 2017, 2018 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <boost/mp11/algorithm.hpp>
  10. #include <boost/mp11/utility.hpp>
  11. #include <cstddef>
  12. namespace boost
  13. {
  14. namespace mp11
  15. {
  16. // mp_bind_front
  17. template<template<class...> class F, class... T> struct mp_bind_front
  18. {
  19. // the indirection through mp_defer works around the language inability
  20. // to expand U... into a fixed parameter list of an alias template
  21. template<class... U> using fn = typename mp_defer<F, T..., U...>::type;
  22. };
  23. template<class Q, class... T> using mp_bind_front_q = mp_bind_front<Q::template fn, T...>;
  24. // mp_bind_back
  25. template<template<class...> class F, class... T> struct mp_bind_back
  26. {
  27. template<class... U> using fn = typename mp_defer<F, U..., T...>::type;
  28. };
  29. template<class Q, class... T> using mp_bind_back_q = mp_bind_back<Q::template fn, T...>;
  30. // mp_arg
  31. template<std::size_t I> struct mp_arg
  32. {
  33. template<class... T> using fn = mp_at_c<mp_list<T...>, I>;
  34. };
  35. using _1 = mp_arg<0>;
  36. using _2 = mp_arg<1>;
  37. using _3 = mp_arg<2>;
  38. using _4 = mp_arg<3>;
  39. using _5 = mp_arg<4>;
  40. using _6 = mp_arg<5>;
  41. using _7 = mp_arg<6>;
  42. using _8 = mp_arg<7>;
  43. using _9 = mp_arg<8>;
  44. // mp_bind
  45. template<template<class...> class F, class... T> struct mp_bind;
  46. namespace detail
  47. {
  48. template<class V, class... T> struct eval_bound_arg
  49. {
  50. using type = V;
  51. };
  52. template<std::size_t I, class... T> struct eval_bound_arg<mp_arg<I>, T...>
  53. {
  54. using type = typename mp_arg<I>::template fn<T...>;
  55. };
  56. template<template<class...> class F, class... U, class... T> struct eval_bound_arg<mp_bind<F, U...>, T...>
  57. {
  58. using type = typename mp_bind<F, U...>::template fn<T...>;
  59. };
  60. template<template<class...> class F, class... U, class... T> struct eval_bound_arg<mp_bind_front<F, U...>, T...>
  61. {
  62. using type = typename mp_bind_front<F, U...>::template fn<T...>;
  63. };
  64. template<template<class...> class F, class... U, class... T> struct eval_bound_arg<mp_bind_back<F, U...>, T...>
  65. {
  66. using type = typename mp_bind_back<F, U...>::template fn<T...>;
  67. };
  68. } // namespace detail
  69. template<template<class...> class F, class... T> struct mp_bind
  70. {
  71. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, == 1915 )
  72. private:
  73. template<class... U> struct _f { using type = F<typename detail::eval_bound_arg<T, U...>::type...>; };
  74. public:
  75. template<class... U> using fn = typename _f<U...>::type;
  76. #else
  77. template<class... U> using fn = F<typename detail::eval_bound_arg<T, U...>::type...>;
  78. #endif
  79. };
  80. template<class Q, class... T> using mp_bind_q = mp_bind<Q::template fn, T...>;
  81. } // namespace mp11
  82. } // namespace boost
  83. #endif // #ifndef BOOST_MP11_BIND_HPP_INCLUDED