utility.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef BOOST_MP11_UTILITY_HPP_INCLUDED
  2. #define BOOST_MP11_UTILITY_HPP_INCLUDED
  3. // Copyright 2015, 2017, 2019 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/integral.hpp>
  10. #include <boost/mp11/detail/config.hpp>
  11. namespace boost
  12. {
  13. namespace mp11
  14. {
  15. // mp_identity
  16. template<class T> struct mp_identity
  17. {
  18. using type = T;
  19. };
  20. // mp_identity_t
  21. template<class T> using mp_identity_t = typename mp_identity<T>::type;
  22. // mp_inherit
  23. template<class... T> struct mp_inherit: T... {};
  24. // mp_if, mp_if_c
  25. namespace detail
  26. {
  27. template<bool C, class T, class... E> struct mp_if_c_impl
  28. {
  29. };
  30. template<class T, class... E> struct mp_if_c_impl<true, T, E...>
  31. {
  32. using type = T;
  33. };
  34. template<class T, class E> struct mp_if_c_impl<false, T, E>
  35. {
  36. using type = E;
  37. };
  38. } // namespace detail
  39. template<bool C, class T, class... E> using mp_if_c = typename detail::mp_if_c_impl<C, T, E...>::type;
  40. template<class C, class T, class... E> using mp_if = typename detail::mp_if_c_impl<static_cast<bool>(C::value), T, E...>::type;
  41. // mp_valid
  42. #if BOOST_MP11_WORKAROUND( BOOST_MP11_INTEL, != 0 ) // tested at 1800
  43. // contributed by Roland Schulz in https://github.com/boostorg/mp11/issues/17
  44. namespace detail
  45. {
  46. template<class...> using void_t = void;
  47. template<class, template<class...> class F, class... T>
  48. struct mp_valid_impl: mp_false {};
  49. template<template<class...> class F, class... T>
  50. struct mp_valid_impl<void_t<F<T...>>, F, T...>: mp_true {};
  51. } // namespace detail
  52. template<template<class...> class F, class... T> using mp_valid = typename detail::mp_valid_impl<void, F, T...>;
  53. #else
  54. // implementation by Bruno Dutra (by the name is_evaluable)
  55. namespace detail
  56. {
  57. template<template<class...> class F, class... T> struct mp_valid_impl
  58. {
  59. template<template<class...> class G, class = G<T...>> static mp_true check(int);
  60. template<template<class...> class> static mp_false check(...);
  61. using type = decltype(check<F>(0));
  62. };
  63. } // namespace detail
  64. template<template<class...> class F, class... T> using mp_valid = typename detail::mp_valid_impl<F, T...>::type;
  65. #endif
  66. template<class Q, class... T> using mp_valid_q = mp_valid<Q::template fn, T...>;
  67. // mp_defer
  68. namespace detail
  69. {
  70. template<template<class...> class F, class... T> struct mp_defer_impl
  71. {
  72. using type = F<T...>;
  73. };
  74. struct mp_no_type
  75. {
  76. };
  77. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  78. template<template<class...> class F, class... T> struct mp_defer_cuda_workaround
  79. {
  80. using type = mp_if<mp_valid<F, T...>, detail::mp_defer_impl<F, T...>, detail::mp_no_type>;
  81. };
  82. #endif
  83. } // namespace detail
  84. #if BOOST_MP11_WORKAROUND( BOOST_MP11_CUDA, >= 9000000 && BOOST_MP11_CUDA < 10000000 )
  85. template<template<class...> class F, class... T> using mp_defer = typename detail::mp_defer_cuda_workaround< F, T...>::type;
  86. #else
  87. template<template<class...> class F, class... T> using mp_defer = mp_if<mp_valid<F, T...>, detail::mp_defer_impl<F, T...>, detail::mp_no_type>;
  88. #endif
  89. // mp_eval_if, mp_eval_if_c
  90. namespace detail
  91. {
  92. template<bool C, class T, template<class...> class F, class... U> struct mp_eval_if_c_impl;
  93. template<class T, template<class...> class F, class... U> struct mp_eval_if_c_impl<true, T, F, U...>
  94. {
  95. using type = T;
  96. };
  97. template<class T, template<class...> class F, class... U> struct mp_eval_if_c_impl<false, T, F, U...>: mp_defer<F, U...>
  98. {
  99. };
  100. } // namespace detail
  101. template<bool C, class T, template<class...> class F, class... U> using mp_eval_if_c = typename detail::mp_eval_if_c_impl<C, T, F, U...>::type;
  102. template<class C, class T, template<class...> class F, class... U> using mp_eval_if = typename detail::mp_eval_if_c_impl<static_cast<bool>(C::value), T, F, U...>::type;
  103. template<class C, class T, class Q, class... U> using mp_eval_if_q = typename detail::mp_eval_if_c_impl<static_cast<bool>(C::value), T, Q::template fn, U...>::type;
  104. // mp_eval_if_not
  105. template<class C, class T, template<class...> class F, class... U> using mp_eval_if_not = mp_eval_if<mp_not<C>, T, F, U...>;
  106. template<class C, class T, class Q, class... U> using mp_eval_if_not_q = mp_eval_if<mp_not<C>, T, Q::template fn, U...>;
  107. // mp_eval_or
  108. template<class T, template<class...> class F, class... U> using mp_eval_or = mp_eval_if_not<mp_valid<F, U...>, T, F, U...>;
  109. template<class T, class Q, class... U> using mp_eval_or_q = mp_eval_or<T, Q::template fn, U...>;
  110. // mp_cond
  111. // so elegant; so doesn't work
  112. // template<class C, class T, class... E> using mp_cond = mp_eval_if<C, T, mp_cond, E...>;
  113. namespace detail
  114. {
  115. template<class C, class T, class... E> struct mp_cond_impl;
  116. } // namespace detail
  117. template<class C, class T, class... E> using mp_cond = typename detail::mp_cond_impl<C, T, E...>::type;
  118. namespace detail
  119. {
  120. template<class C, class T, class... E> using mp_cond_ = mp_eval_if<C, T, mp_cond, E...>;
  121. template<class C, class T, class... E> struct mp_cond_impl: mp_defer<mp_cond_, C, T, E...>
  122. {
  123. };
  124. } // namespace detail
  125. // mp_quote
  126. template<template<class...> class F> struct mp_quote
  127. {
  128. // the indirection through mp_defer works around the language inability
  129. // to expand T... into a fixed parameter list of an alias template
  130. template<class... T> using fn = typename mp_defer<F, T...>::type;
  131. };
  132. // mp_quote_trait
  133. template<template<class...> class F> struct mp_quote_trait
  134. {
  135. template<class... T> using fn = typename F<T...>::type;
  136. };
  137. // mp_invoke_q
  138. #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
  139. namespace detail
  140. {
  141. template<class Q, class... T> struct mp_invoke_q_impl: mp_defer<Q::template fn, T...> {};
  142. } // namespace detail
  143. template<class Q, class... T> using mp_invoke_q = typename detail::mp_invoke_q_impl<Q, T...>::type;
  144. #elif BOOST_MP11_WORKAROUND( BOOST_MP11_GCC, < 50000 )
  145. template<class Q, class... T> using mp_invoke_q = typename mp_defer<Q::template fn, T...>::type;
  146. #else
  147. template<class Q, class... T> using mp_invoke_q = typename Q::template fn<T...>;
  148. #endif
  149. // old name for mp_invoke_q retained for compatibility, but deprecated
  150. template<class Q, class... T> using mp_invoke BOOST_MP11_DEPRECATED("please use mp_invoke_q") = mp_invoke_q<Q, T...>;
  151. // mp_not_fn<P>
  152. template<template<class...> class P> struct mp_not_fn
  153. {
  154. template<class... T> using fn = mp_not< mp_invoke_q<mp_quote<P>, T...> >;
  155. };
  156. template<class Q> using mp_not_fn_q = mp_not_fn<Q::template fn>;
  157. } // namespace mp11
  158. } // namespace boost
  159. #endif // #ifndef BOOST_MP11_UTILITY_HPP_INCLUDED