result_of.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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)
  62. namespace detail {
  63. BOOST_MPL_HAS_XXX_TRAIT_DEF(result_type)
  64. // Work around a nvcc bug by only defining has_result when it's needed.
  65. #ifdef BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK
  66. BOOST_MPL_HAS_XXX_TEMPLATE_DEF(result)
  67. #endif
  68. template<typename F, typename FArgs, bool HasResultType> struct tr1_result_of_impl;
  69. template<typename F> struct cpp0x_result_of;
  70. #ifdef BOOST_NO_SFINAE_EXPR
  71. // There doesn't seem to be any other way to turn this off such that the presence of
  72. // the user-defined operator,() below doesn't cause spurious warning all over the place,
  73. // so unconditionally turn it off.
  74. #if BOOST_MSVC
  75. # pragma warning(disable: 4913) // user defined binary operator ',' exists but no overload could convert all operands, default built-in binary operator ',' used
  76. #endif
  77. struct result_of_private_type {};
  78. struct result_of_weird_type {
  79. friend result_of_private_type operator,(result_of_private_type, result_of_weird_type);
  80. };
  81. typedef char result_of_yes_type; // sizeof(result_of_yes_type) == 1
  82. typedef char (&result_of_no_type)[2]; // sizeof(result_of_no_type) == 2
  83. template<typename T>
  84. result_of_no_type result_of_is_private_type(T const &);
  85. result_of_yes_type result_of_is_private_type(result_of_private_type);
  86. template<typename C>
  87. struct result_of_callable_class : C {
  88. result_of_callable_class();
  89. typedef result_of_private_type const &(*pfn_t)(...);
  90. operator pfn_t() const volatile;
  91. };
  92. template<typename C>
  93. struct result_of_wrap_callable_class {
  94. typedef result_of_callable_class<C> type;
  95. };
  96. template<typename C>
  97. struct result_of_wrap_callable_class<C const> {
  98. typedef result_of_callable_class<C> const type;
  99. };
  100. template<typename C>
  101. struct result_of_wrap_callable_class<C volatile> {
  102. typedef result_of_callable_class<C> volatile type;
  103. };
  104. template<typename C>
  105. struct result_of_wrap_callable_class<C const volatile> {
  106. typedef result_of_callable_class<C> const volatile type;
  107. };
  108. template<typename C>
  109. struct result_of_wrap_callable_class<C &> {
  110. typedef typename result_of_wrap_callable_class<C>::type &type;
  111. };
  112. template<typename F, bool TestCallability = true> struct cpp0x_result_of_impl;
  113. #else // BOOST_NO_SFINAE_EXPR
  114. template<typename T>
  115. struct result_of_always_void
  116. {
  117. typedef void type;
  118. };
  119. template<typename F, typename Enable = void> struct cpp0x_result_of_impl {};
  120. #endif // BOOST_NO_SFINAE_EXPR
  121. template<typename F>
  122. struct result_of_void_impl
  123. {
  124. typedef void type;
  125. };
  126. template<typename R>
  127. struct result_of_void_impl<R (*)(void)>
  128. {
  129. typedef R type;
  130. };
  131. template<typename R>
  132. struct result_of_void_impl<R (&)(void)>
  133. {
  134. typedef R type;
  135. };
  136. // Determine the return type of a function pointer or pointer to member.
  137. template<typename F, typename FArgs>
  138. struct result_of_pointer
  139. : tr1_result_of_impl<typename remove_cv<F>::type, FArgs, false> { };
  140. template<typename F, typename FArgs>
  141. struct tr1_result_of_impl<F, FArgs, true>
  142. {
  143. typedef typename F::result_type type;
  144. };
  145. template<typename FArgs>
  146. struct is_function_with_no_args : mpl::false_ {};
  147. template<typename F>
  148. struct is_function_with_no_args<F(void)> : mpl::true_ {};
  149. template<typename F, typename FArgs>
  150. struct result_of_nested_result : F::template result<FArgs>
  151. {};
  152. template<typename F, typename FArgs>
  153. struct tr1_result_of_impl<F, FArgs, false>
  154. : mpl::if_<is_function_with_no_args<FArgs>,
  155. result_of_void_impl<F>,
  156. result_of_nested_result<F, FArgs> >::type
  157. {};
  158. } // end namespace detail
  159. #define BOOST_PP_ITERATION_PARAMS_1 (3,(0,BOOST_RESULT_OF_NUM_ARGS,<boost/utility/detail/result_of_iterate.hpp>))
  160. #include BOOST_PP_ITERATE()
  161. #else
  162. # define BOOST_NO_RESULT_OF 1
  163. #endif
  164. }
  165. #endif // BOOST_RESULT_OF_HPP