result_of.hpp 7.9 KB

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