implicit.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*=============================================================================
  2. Copyright (c) 2012 Paul Fultz II
  3. implicit.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_FUNCTION_IMPLICIT_H
  8. #define BOOST_HOF_GUARD_FUNCTION_IMPLICIT_H
  9. /// implicit
  10. /// ========
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `implicit` adaptor is a static function adaptor that uses the type
  16. /// that the return value can be converted to, in order to determine the type
  17. /// of the template parameter. In essence, it will deduce the type for the
  18. /// template parameter using the type of variable the result is assigned to.
  19. /// Since it is a static function adaptor, the function must be default
  20. /// constructible.
  21. ///
  22. /// Synopsis
  23. /// --------
  24. ///
  25. /// template<template <class...> class F>
  26. /// class implicit<F>;
  27. ///
  28. /// Semantics
  29. /// ---------
  30. ///
  31. /// assert(T(implicit<F>()(xs...)) == F<T>()(xs...));
  32. ///
  33. /// Requirements
  34. /// ------------
  35. ///
  36. /// F must be a template class, that is a:
  37. ///
  38. /// * [ConstFunctionObject](ConstFunctionObject)
  39. /// * DefaultConstructible
  40. ///
  41. /// Example
  42. /// -------
  43. ///
  44. /// #include <boost/hof.hpp>
  45. /// #include <cassert>
  46. /// using namespace boost::hof;
  47. ///
  48. /// template<class T>
  49. /// struct auto_caster
  50. /// {
  51. /// template<class U>
  52. /// T operator()(U x)
  53. /// {
  54. /// return T(x);
  55. /// }
  56. /// };
  57. ///
  58. /// static constexpr implicit<auto_caster> auto_cast = {};
  59. ///
  60. /// struct auto_caster_foo
  61. /// {
  62. /// int i;
  63. /// explicit auto_caster_foo(int i_) : i(i_) {}
  64. ///
  65. /// };
  66. ///
  67. /// int main() {
  68. /// float f = 1.5;
  69. /// int i = auto_cast(f);
  70. /// auto_caster_foo x = auto_cast(1);
  71. /// assert(1 == i);
  72. /// assert(1 == x.i);
  73. /// }
  74. ///
  75. #include <boost/hof/pack.hpp>
  76. #include <boost/hof/detail/result_of.hpp>
  77. namespace boost { namespace hof { namespace detail {
  78. template<class F, class Pack, class X, class=void>
  79. struct is_implicit_callable
  80. : std::false_type
  81. {};
  82. #if BOOST_HOF_NO_EXPRESSION_SFINAE
  83. template<class F, class Pack, class X>
  84. struct is_implicit_callable<F, Pack, X, typename std::enable_if<
  85. std::is_convertible<typename result_of<Pack, id_<F>>::type, X>::value
  86. >::type>
  87. : std::true_type
  88. {};
  89. #else
  90. template<class F, class Pack, class X>
  91. struct is_implicit_callable<F, Pack, X, typename std::enable_if<
  92. std::is_convertible<decltype(std::declval<Pack>()(std::declval<F>())), X>::value
  93. >::type>
  94. : std::true_type
  95. {};
  96. #endif
  97. }
  98. template<template <class...> class F>
  99. struct implicit
  100. {
  101. template<class Pack>
  102. struct invoker
  103. {
  104. Pack p;
  105. constexpr invoker(Pack pp) BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(Pack, Pack&&)
  106. : p(boost::hof::move(pp))
  107. {}
  108. template<class X, class=typename std::enable_if<detail::is_implicit_callable<F<X>, Pack, X>::value>::type>
  109. constexpr operator X() const BOOST_HOF_NOEXCEPT(noexcept(p(F<X>())))
  110. {
  111. return p(F<X>());
  112. }
  113. #if !(defined(__GNUC__) && !defined (__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7)
  114. invoker (const invoker&) = delete;
  115. invoker& operator= (const invoker&) = delete;
  116. private:
  117. friend struct implicit;
  118. invoker (invoker&&) = default;
  119. #endif
  120. };
  121. struct make_invoker
  122. {
  123. template<class Pack>
  124. constexpr invoker<Pack> operator()(Pack p) const BOOST_HOF_NOEXCEPT(noexcept(invoker<Pack>(boost::hof::move(p))))
  125. {
  126. return invoker<Pack>(boost::hof::move(p));
  127. }
  128. };
  129. template<class... Ts>
  130. constexpr auto operator()(Ts&&... xs) const
  131. BOOST_HOF_RETURNS
  132. (
  133. BOOST_HOF_RETURNS_CONSTRUCT(make_invoker)()(boost::hof::pack_basic(BOOST_HOF_FORWARD(Ts)(xs)...))
  134. );
  135. };
  136. }} // namespace boost::hof
  137. #endif