result_of.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Boost result_of library
  2. // Copyright Douglas Gregor 2004. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org/libs/utility
  7. #ifndef BOOST_RESULT_OF_HPP
  8. #define BOOST_RESULT_OF_HPP
  9. #include <boost/config.hpp>
  10. #include <boost/preprocessor/cat.hpp>
  11. #include <boost/preprocessor/iteration/iterate.hpp>
  12. #include <boost/preprocessor/repetition/enum_params.hpp>
  13. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  14. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  15. #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
  16. #include <boost/preprocessor/facilities/intercept.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/mpl/has_xxx.hpp>
  19. #include <boost/mpl/if.hpp>
  20. #include <boost/mpl/eval_if.hpp>
  21. #include <boost/mpl/bool.hpp>
  22. #include <boost/mpl/identity.hpp>
  23. #include <boost/mpl/or.hpp>
  24. #include <boost/type_traits/is_class.hpp>
  25. #include <boost/type_traits/is_pointer.hpp>
  26. #include <boost/type_traits/is_member_function_pointer.hpp>
  27. #include <boost/type_traits/remove_cv.hpp>
  28. #include <boost/type_traits/remove_reference.hpp>
  29. #include <boost/utility/declval.hpp>
  30. #include <boost/utility/enable_if.hpp>
  31. #ifndef BOOST_RESULT_OF_NUM_ARGS
  32. # define BOOST_RESULT_OF_NUM_ARGS 16
  33. #endif
  34. // Use the decltype-based version of result_of by default if the compiler
  35. // supports N3276 <http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2011/n3276.pdf>.
  36. // The user can force the choice by defining BOOST_RESULT_OF_USE_DECLTYPE,
  37. // BOOST_RESULT_OF_USE_TR1, or BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK but not more than one!
  38. #if (defined(BOOST_RESULT_OF_USE_DECLTYPE) && defined(BOOST_RESULT_OF_USE_TR1)) || \
  39. (defined(BOOST_RESULT_OF_USE_DECLTYPE) && defined(BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK)) || \
  40. (defined(BOOST_RESULT_OF_USE_TR1) && defined(BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK))
  41. # error More than one of BOOST_RESULT_OF_USE_DECLTYPE, BOOST_RESULT_OF_USE_TR1 and \
  42. BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK cannot be defined at the same time.
  43. #endif
  44. #if defined(BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK) && defined(BOOST_MPL_CFG_NO_HAS_XXX_TEMPLATE)
  45. # error Cannot fallback to decltype if BOOST_MPL_CFG_NO_HAS_XXX_TEMPLATE is not defined.
  46. #endif
  47. #ifndef BOOST_RESULT_OF_USE_TR1
  48. # ifndef BOOST_RESULT_OF_USE_DECLTYPE
  49. # ifndef BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK
  50. # ifndef BOOST_NO_CXX11_DECLTYPE_N3276 // this implies !defined(BOOST_NO_CXX11_DECLTYPE)
  51. # define BOOST_RESULT_OF_USE_DECLTYPE
  52. # else
  53. # define BOOST_RESULT_OF_USE_TR1
  54. # endif
  55. # endif
  56. # endif
  57. #endif
  58. namespace boost {
  59. template<typename F> struct result_of;
  60. template<typename F> struct tr1_result_of; // a TR1-style implementation of result_of
  61. #if !defined(BOOST_NO_SFINAE) && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  62. namespace detail {
  63. BOOST_MPL_HAS_XXX_TRAIT_DEF(result_type)
  64. BOOST_MPL_HAS_XXX_TEMPLATE_DEF(result)
  65. template<typename F, typename FArgs, bool HasResultType> struct tr1_result_of_impl;
  66. template<typename F> struct cpp0x_result_of;
  67. #ifdef BOOST_NO_SFINAE_EXPR
  68. // There doesn't seem to be any other way to turn this off such that the presence of
  69. // the user-defined operator,() below doesn't cause spurious warning all over the place,
  70. // so unconditionally turn it off.
  71. #if BOOST_MSVC
  72. # pragma warning(disable: 4913) // user defined binary operator ',' exists but no overload could convert all operands, default built-in binary operator ',' used
  73. #endif
  74. struct result_of_private_type {};
  75. struct result_of_weird_type {
  76. friend result_of_private_type operator,(result_of_private_type, result_of_weird_type);
  77. };
  78. typedef char result_of_yes_type; // sizeof(result_of_yes_type) == 1
  79. typedef char (&result_of_no_type)[2]; // sizeof(result_of_no_type) == 2
  80. template<typename T>
  81. result_of_no_type result_of_is_private_type(T const &);
  82. result_of_yes_type result_of_is_private_type(result_of_private_type);
  83. template<typename C>
  84. struct result_of_callable_class : C {
  85. result_of_callable_class();
  86. typedef result_of_private_type const &(*pfn_t)(...);
  87. operator pfn_t() const volatile;
  88. };
  89. template<typename C>
  90. struct result_of_wrap_callable_class {
  91. typedef result_of_callable_class<C> type;
  92. };
  93. template<typename C>
  94. struct result_of_wrap_callable_class<C const> {
  95. typedef result_of_callable_class<C> const type;
  96. };
  97. template<typename C>
  98. struct result_of_wrap_callable_class<C volatile> {
  99. typedef result_of_callable_class<C> volatile type;
  100. };
  101. template<typename C>
  102. struct result_of_wrap_callable_class<C const volatile> {
  103. typedef result_of_callable_class<C> const volatile type;
  104. };
  105. template<typename C>
  106. struct result_of_wrap_callable_class<C &> {
  107. typedef typename result_of_wrap_callable_class<C>::type &type;
  108. };
  109. template<typename F, bool TestCallability = true> struct cpp0x_result_of_impl;
  110. #else // BOOST_NO_SFINAE_EXPR
  111. template<typename T>
  112. struct result_of_always_void
  113. {
  114. typedef void type;
  115. };
  116. template<typename F, typename Enable = void> struct cpp0x_result_of_impl {};
  117. #endif // BOOST_NO_SFINAE_EXPR
  118. template<typename F>
  119. struct result_of_void_impl
  120. {
  121. typedef void type;
  122. };
  123. template<typename R>
  124. struct result_of_void_impl<R (*)(void)>
  125. {
  126. typedef R type;
  127. };
  128. template<typename R>
  129. struct result_of_void_impl<R (&)(void)>
  130. {
  131. typedef R type;
  132. };
  133. // Determine the return type of a function pointer or pointer to member.
  134. template<typename F, typename FArgs>
  135. struct result_of_pointer
  136. : tr1_result_of_impl<typename remove_cv<F>::type, FArgs, false> { };
  137. template<typename F, typename FArgs>
  138. struct tr1_result_of_impl<F, FArgs, true>
  139. {
  140. typedef typename F::result_type type;
  141. };
  142. template<typename FArgs>
  143. struct is_function_with_no_args : mpl::false_ {};
  144. template<typename F>
  145. struct is_function_with_no_args<F(void)> : mpl::true_ {};
  146. template<typename F, typename FArgs>
  147. struct result_of_nested_result : F::template result<FArgs>
  148. {};
  149. template<typename F, typename FArgs>
  150. struct tr1_result_of_impl<F, FArgs, false>
  151. : mpl::if_<is_function_with_no_args<FArgs>,
  152. result_of_void_impl<F>,
  153. result_of_nested_result<F, FArgs> >::type
  154. {};
  155. } // end namespace detail
  156. #define BOOST_PP_ITERATION_PARAMS_1 (3,(0,BOOST_RESULT_OF_NUM_ARGS,<boost/utility/detail/result_of_iterate.hpp>))
  157. #include BOOST_PP_ITERATE()
  158. #else
  159. # define BOOST_NO_RESULT_OF 1
  160. #endif
  161. }
  162. #endif // BOOST_RESULT_OF_HPP