bind.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #if defined(_MSC_VER) || defined(__GNUC__)
  13. # pragma push_macro( "I" )
  14. # undef I
  15. #endif
  16. namespace boost
  17. {
  18. namespace mp11
  19. {
  20. // mp_bind_front
  21. template<template<class...> class F, class... T> struct mp_bind_front
  22. {
  23. // the indirection through mp_defer works around the language inability
  24. // to expand U... into a fixed parameter list of an alias template
  25. template<class... U> using fn = typename mp_defer<F, T..., U...>::type;
  26. };
  27. template<class Q, class... T> using mp_bind_front_q = mp_bind_front<Q::template fn, T...>;
  28. // mp_bind_back
  29. template<template<class...> class F, class... T> struct mp_bind_back
  30. {
  31. template<class... U> using fn = typename mp_defer<F, U..., T...>::type;
  32. };
  33. template<class Q, class... T> using mp_bind_back_q = mp_bind_back<Q::template fn, T...>;
  34. // mp_arg
  35. template<std::size_t I> struct mp_arg
  36. {
  37. template<class... T> using fn = mp_at_c<mp_list<T...>, I>;
  38. };
  39. using _1 = mp_arg<0>;
  40. using _2 = mp_arg<1>;
  41. using _3 = mp_arg<2>;
  42. using _4 = mp_arg<3>;
  43. using _5 = mp_arg<4>;
  44. using _6 = mp_arg<5>;
  45. using _7 = mp_arg<6>;
  46. using _8 = mp_arg<7>;
  47. using _9 = mp_arg<8>;
  48. // mp_bind
  49. template<template<class...> class F, class... T> struct mp_bind;
  50. namespace detail
  51. {
  52. template<class V, class... T> struct eval_bound_arg
  53. {
  54. using type = V;
  55. };
  56. template<std::size_t I, class... T> struct eval_bound_arg<mp_arg<I>, T...>
  57. {
  58. using type = typename mp_arg<I>::template fn<T...>;
  59. };
  60. template<template<class...> class F, class... U, class... T> struct eval_bound_arg<mp_bind<F, U...>, T...>
  61. {
  62. using type = typename mp_bind<F, U...>::template fn<T...>;
  63. };
  64. template<template<class...> class F, class... U, class... T> struct eval_bound_arg<mp_bind_front<F, U...>, T...>
  65. {
  66. using type = typename mp_bind_front<F, U...>::template fn<T...>;
  67. };
  68. template<template<class...> class F, class... U, class... T> struct eval_bound_arg<mp_bind_back<F, U...>, T...>
  69. {
  70. using type = typename mp_bind_back<F, U...>::template fn<T...>;
  71. };
  72. } // namespace detail
  73. template<template<class...> class F, class... T> struct mp_bind
  74. {
  75. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, == 1915 )
  76. private:
  77. template<class... U> struct _f { using type = F<typename detail::eval_bound_arg<T, U...>::type...>; };
  78. public:
  79. template<class... U> using fn = typename _f<U...>::type;
  80. #else
  81. template<class... U> using fn = F<typename detail::eval_bound_arg<T, U...>::type...>;
  82. #endif
  83. };
  84. template<class Q, class... T> using mp_bind_q = mp_bind<Q::template fn, T...>;
  85. } // namespace mp11
  86. } // namespace boost
  87. #if defined(_MSC_VER) || defined(__GNUC__)
  88. # pragma pop_macro( "I" )
  89. #endif
  90. #endif // #ifndef BOOST_MP11_BIND_HPP_INCLUDED