result_of.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/type_traits/is_class.hpp>
  19. #include <boost/type_traits/is_pointer.hpp>
  20. #include <boost/type_traits/is_member_function_pointer.hpp>
  21. #include <boost/type_traits/remove_cv.hpp>
  22. #include <boost/type_traits/remove_reference.hpp>
  23. #include <boost/type_traits/declval.hpp>
  24. #include <boost/type_traits/conditional.hpp>
  25. #include <boost/type_traits/type_identity.hpp>
  26. #include <boost/type_traits/integral_constant.hpp>
  27. #include <boost/core/enable_if.hpp>
  28. #ifndef BOOST_RESULT_OF_NUM_ARGS
  29. # define BOOST_RESULT_OF_NUM_ARGS 16
  30. #endif
  31. // Use the decltype-based version of result_of by default if the compiler
  32. // supports N3276 <http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2011/n3276.pdf>.
  33. // The user can force the choice by defining BOOST_RESULT_OF_USE_DECLTYPE,
  34. // BOOST_RESULT_OF_USE_TR1, or BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK but not more than one!
  35. #if (defined(BOOST_RESULT_OF_USE_DECLTYPE) && defined(BOOST_RESULT_OF_USE_TR1)) || \
  36. (defined(BOOST_RESULT_OF_USE_DECLTYPE) && defined(BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK)) || \
  37. (defined(BOOST_RESULT_OF_USE_TR1) && defined(BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK))
  38. # error More than one of BOOST_RESULT_OF_USE_DECLTYPE, BOOST_RESULT_OF_USE_TR1 and \
  39. BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK cannot be defined at the same time.
  40. #endif
  41. #ifndef BOOST_RESULT_OF_USE_TR1
  42. # ifndef BOOST_RESULT_OF_USE_DECLTYPE
  43. # ifndef BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK
  44. # ifndef BOOST_NO_CXX11_DECLTYPE_N3276 // this implies !defined(BOOST_NO_CXX11_DECLTYPE)
  45. # define BOOST_RESULT_OF_USE_DECLTYPE
  46. # else
  47. # define BOOST_RESULT_OF_USE_TR1
  48. # endif
  49. # endif
  50. # endif
  51. #endif
  52. namespace boost {
  53. template<typename F> struct result_of;
  54. template<typename F> struct tr1_result_of; // a TR1-style implementation of result_of
  55. #if !defined(BOOST_NO_SFINAE)
  56. namespace detail {
  57. typedef char result_of_yes_type; // sizeof(result_of_yes_type) == 1
  58. typedef char (&result_of_no_type)[2]; // sizeof(result_of_no_type) == 2
  59. template<class T> struct result_of_has_type {};
  60. template<class T> struct result_of_has_result_type_impl
  61. {
  62. template<class U> static result_of_yes_type f( result_of_has_type<typename U::result_type>* );
  63. template<class U> static result_of_no_type f( ... );
  64. typedef boost::integral_constant<bool, sizeof(f<T>(0)) == sizeof(result_of_yes_type)> type;
  65. };
  66. template<class T> struct result_of_has_result_type: result_of_has_result_type_impl<T>::type
  67. {
  68. };
  69. // Work around a nvcc bug by only defining has_result when it's needed.
  70. #ifdef BOOST_RESULT_OF_USE_TR1_WITH_DECLTYPE_FALLBACK
  71. template<template<class> class C> struct result_of_has_template {};
  72. template<class T> struct result_of_has_result_impl
  73. {
  74. template<class U> static result_of_yes_type f( result_of_has_template<U::template result>* );
  75. template<class U> static result_of_no_type f( ... );
  76. typedef boost::integral_constant<bool, sizeof(f<T>(0)) == sizeof(result_of_yes_type)> type;
  77. };
  78. template<class T> struct result_of_has_result: result_of_has_result_impl<T>::type
  79. {
  80. };
  81. #endif
  82. template<typename F, typename FArgs, bool HasResultType> struct tr1_result_of_impl;
  83. template<typename F> struct cpp0x_result_of;
  84. #ifdef BOOST_NO_SFINAE_EXPR
  85. // There doesn't seem to be any other way to turn this off such that the presence of
  86. // the user-defined operator,() below doesn't cause spurious warning all over the place,
  87. // so unconditionally turn it off.
  88. #if BOOST_MSVC
  89. # pragma warning(disable: 4913) // user defined binary operator ',' exists but no overload could convert all operands, default built-in binary operator ',' used
  90. #endif
  91. struct result_of_private_type {};
  92. struct result_of_weird_type {
  93. friend result_of_private_type operator,(result_of_private_type, result_of_weird_type);
  94. };
  95. template<typename T>
  96. result_of_no_type result_of_is_private_type(T const &);
  97. result_of_yes_type result_of_is_private_type(result_of_private_type);
  98. template<typename C>
  99. struct result_of_callable_class : C {
  100. result_of_callable_class();
  101. typedef result_of_private_type const &(*pfn_t)(...);
  102. operator pfn_t() const volatile;
  103. };
  104. template<typename C>
  105. struct result_of_wrap_callable_class {
  106. typedef result_of_callable_class<C> type;
  107. };
  108. template<typename C>
  109. struct result_of_wrap_callable_class<C const> {
  110. typedef result_of_callable_class<C> const type;
  111. };
  112. template<typename C>
  113. struct result_of_wrap_callable_class<C volatile> {
  114. typedef result_of_callable_class<C> volatile type;
  115. };
  116. template<typename C>
  117. struct result_of_wrap_callable_class<C const volatile> {
  118. typedef result_of_callable_class<C> const volatile type;
  119. };
  120. template<typename C>
  121. struct result_of_wrap_callable_class<C &> {
  122. typedef typename result_of_wrap_callable_class<C>::type &type;
  123. };
  124. template<typename F, bool TestCallability = true> struct cpp0x_result_of_impl;
  125. #else // BOOST_NO_SFINAE_EXPR
  126. template<typename T>
  127. struct result_of_always_void
  128. {
  129. typedef void type;
  130. };
  131. template<typename F, typename Enable = void> struct cpp0x_result_of_impl {};
  132. #endif // BOOST_NO_SFINAE_EXPR
  133. template<typename F>
  134. struct result_of_void_impl
  135. {
  136. typedef void type;
  137. };
  138. template<typename R>
  139. struct result_of_void_impl<R (*)(void)>
  140. {
  141. typedef R type;
  142. };
  143. template<typename R>
  144. struct result_of_void_impl<R (&)(void)>
  145. {
  146. typedef R type;
  147. };
  148. // Determine the return type of a function pointer or pointer to member.
  149. template<typename F, typename FArgs>
  150. struct result_of_pointer
  151. : tr1_result_of_impl<typename remove_cv<F>::type, FArgs, false> { };
  152. template<typename F, typename FArgs>
  153. struct tr1_result_of_impl<F, FArgs, true>
  154. {
  155. typedef typename F::result_type type;
  156. };
  157. template<typename FArgs>
  158. struct is_function_with_no_args : false_type {};
  159. template<typename F>
  160. struct is_function_with_no_args<F(void)> : true_type {};
  161. template<typename F, typename FArgs>
  162. struct result_of_nested_result : F::template result<FArgs>
  163. {};
  164. template<typename F, typename FArgs>
  165. struct tr1_result_of_impl<F, FArgs, false>
  166. : conditional<is_function_with_no_args<FArgs>::value,
  167. result_of_void_impl<F>,
  168. result_of_nested_result<F, FArgs> >::type
  169. {};
  170. } // end namespace detail
  171. #define BOOST_PP_ITERATION_PARAMS_1 (3,(0,BOOST_RESULT_OF_NUM_ARGS,<boost/utility/detail/result_of_iterate.hpp>))
  172. #include BOOST_PP_ITERATE()
  173. #if 0
  174. // inform dependency trackers, as they can't see through macro includes
  175. #include <boost/utility/detail/result_of_iterate.hpp>
  176. #endif
  177. #else
  178. # define BOOST_NO_RESULT_OF 1
  179. #endif
  180. }
  181. #endif // BOOST_RESULT_OF_HPP