addressof.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. Copyright (C) 2002 Brad King ([email protected])
  3. Douglas Gregor ([email protected])
  4. Copyright (C) 2002, 2008, 2013 Peter Dimov
  5. Copyright (C) 2017 Glen Joseph Fernandes ([email protected])
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENSE_1_0.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef BOOST_CORE_ADDRESSOF_HPP
  11. #define BOOST_CORE_ADDRESSOF_HPP
  12. #include <boost/config.hpp>
  13. #if defined(BOOST_MSVC_FULL_VER) && BOOST_MSVC_FULL_VER >= 190024215
  14. #define BOOST_CORE_HAS_BUILTIN_ADDRESSOF
  15. #elif defined(BOOST_GCC) && BOOST_GCC >= 70000
  16. #define BOOST_CORE_HAS_BUILTIN_ADDRESSOF
  17. #elif defined(__has_builtin)
  18. #if __has_builtin(__builtin_addressof)
  19. #define BOOST_CORE_HAS_BUILTIN_ADDRESSOF
  20. #endif
  21. #endif
  22. #if defined(BOOST_CORE_HAS_BUILTIN_ADDRESSOF)
  23. #if defined(BOOST_NO_CXX11_CONSTEXPR)
  24. #define BOOST_CORE_NO_CONSTEXPR_ADDRESSOF
  25. #endif
  26. namespace boost {
  27. template<class T>
  28. BOOST_CONSTEXPR inline T*
  29. addressof(T& o) BOOST_NOEXCEPT
  30. {
  31. return __builtin_addressof(o);
  32. }
  33. } /* boost */
  34. #else
  35. #include <boost/config/workaround.hpp>
  36. #include <cstddef>
  37. namespace boost {
  38. namespace detail {
  39. template<class T>
  40. class addrof_ref {
  41. public:
  42. BOOST_FORCEINLINE addrof_ref(T& o) BOOST_NOEXCEPT
  43. : o_(o) { }
  44. BOOST_FORCEINLINE operator T&() const BOOST_NOEXCEPT {
  45. return o_;
  46. }
  47. private:
  48. addrof_ref& operator=(const addrof_ref&);
  49. T& o_;
  50. };
  51. template<class T>
  52. struct addrof {
  53. static BOOST_FORCEINLINE T* get(T& o, long) BOOST_NOEXCEPT {
  54. return reinterpret_cast<T*>(&
  55. const_cast<char&>(reinterpret_cast<const volatile char&>(o)));
  56. }
  57. static BOOST_FORCEINLINE T* get(T* p, int) BOOST_NOEXCEPT {
  58. return p;
  59. }
  60. };
  61. #if !defined(BOOST_NO_CXX11_NULLPTR)
  62. #if !defined(BOOST_NO_CXX11_DECLTYPE) && \
  63. (defined(__INTEL_COMPILER) || \
  64. (defined(__clang__) && !defined(_LIBCPP_VERSION)))
  65. typedef decltype(nullptr) addrof_null_t;
  66. #else
  67. typedef std::nullptr_t addrof_null_t;
  68. #endif
  69. template<>
  70. struct addrof<addrof_null_t> {
  71. typedef addrof_null_t type;
  72. static BOOST_FORCEINLINE type* get(type& o, int) BOOST_NOEXCEPT {
  73. return &o;
  74. }
  75. };
  76. template<>
  77. struct addrof<const addrof_null_t> {
  78. typedef const addrof_null_t type;
  79. static BOOST_FORCEINLINE type* get(type& o, int) BOOST_NOEXCEPT {
  80. return &o;
  81. }
  82. };
  83. template<>
  84. struct addrof<volatile addrof_null_t> {
  85. typedef volatile addrof_null_t type;
  86. static BOOST_FORCEINLINE type* get(type& o, int) BOOST_NOEXCEPT {
  87. return &o;
  88. }
  89. };
  90. template<>
  91. struct addrof<const volatile addrof_null_t> {
  92. typedef const volatile addrof_null_t type;
  93. static BOOST_FORCEINLINE type* get(type& o, int) BOOST_NOEXCEPT {
  94. return &o;
  95. }
  96. };
  97. #endif
  98. } /* detail */
  99. #if defined(BOOST_NO_CXX11_SFINAE_EXPR) || \
  100. defined(BOOST_NO_CXX11_CONSTEXPR) || \
  101. defined(BOOST_NO_CXX11_DECLTYPE)
  102. #define BOOST_CORE_NO_CONSTEXPR_ADDRESSOF
  103. template<class T>
  104. BOOST_FORCEINLINE T*
  105. addressof(T& o) BOOST_NOEXCEPT
  106. {
  107. #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x610)) || \
  108. BOOST_WORKAROUND(__SUNPRO_CC, <= 0x5120)
  109. return boost::detail::addrof<T>::get(o, 0);
  110. #else
  111. return boost::detail::addrof<T>::get(boost::detail::addrof_ref<T>(o), 0);
  112. #endif
  113. }
  114. #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  115. namespace detail {
  116. template<class T>
  117. struct addrof_result {
  118. typedef T* type;
  119. };
  120. } /* detail */
  121. template<class T, std::size_t N>
  122. BOOST_FORCEINLINE typename boost::detail::addrof_result<T[N]>::type
  123. addressof(T (&o)[N]) BOOST_NOEXCEPT
  124. {
  125. return &o;
  126. }
  127. #endif
  128. #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
  129. template<class T, std::size_t N>
  130. BOOST_FORCEINLINE
  131. T (*addressof(T (&o)[N]) BOOST_NOEXCEPT)[N]
  132. {
  133. return reinterpret_cast<T(*)[N]>(&o);
  134. }
  135. template<class T, std::size_t N>
  136. BOOST_FORCEINLINE
  137. const T (*addressof(const T (&o)[N]) BOOST_NOEXCEPT)[N]
  138. {
  139. return reinterpret_cast<const T(*)[N]>(&o);
  140. }
  141. #endif
  142. #else
  143. namespace detail {
  144. template<class T>
  145. T addrof_declval() BOOST_NOEXCEPT;
  146. template<class>
  147. struct addrof_void {
  148. typedef void type;
  149. };
  150. template<class T, class E = void>
  151. struct addrof_member_operator {
  152. static constexpr bool value = false;
  153. };
  154. template<class T>
  155. struct addrof_member_operator<T, typename
  156. addrof_void<decltype(addrof_declval<T&>().operator&())>::type> {
  157. static constexpr bool value = true;
  158. };
  159. #if BOOST_WORKAROUND(BOOST_INTEL, < 1600)
  160. struct addrof_addressable { };
  161. addrof_addressable*
  162. operator&(addrof_addressable&) BOOST_NOEXCEPT;
  163. #endif
  164. template<class T, class E = void>
  165. struct addrof_non_member_operator {
  166. static constexpr bool value = false;
  167. };
  168. template<class T>
  169. struct addrof_non_member_operator<T, typename
  170. addrof_void<decltype(operator&(addrof_declval<T&>()))>::type> {
  171. static constexpr bool value = true;
  172. };
  173. template<class T, class E = void>
  174. struct addrof_expression {
  175. static constexpr bool value = false;
  176. };
  177. template<class T>
  178. struct addrof_expression<T,
  179. typename addrof_void<decltype(&addrof_declval<T&>())>::type> {
  180. static constexpr bool value = true;
  181. };
  182. template<class T>
  183. struct addrof_is_constexpr {
  184. static constexpr bool value = addrof_expression<T>::value &&
  185. !addrof_member_operator<T>::value &&
  186. !addrof_non_member_operator<T>::value;
  187. };
  188. template<bool E, class T>
  189. struct addrof_if { };
  190. template<class T>
  191. struct addrof_if<true, T> {
  192. typedef T* type;
  193. };
  194. template<class T>
  195. BOOST_FORCEINLINE
  196. typename addrof_if<!addrof_is_constexpr<T>::value, T>::type
  197. addressof(T& o) BOOST_NOEXCEPT
  198. {
  199. return addrof<T>::get(addrof_ref<T>(o), 0);
  200. }
  201. template<class T>
  202. constexpr BOOST_FORCEINLINE
  203. typename addrof_if<addrof_is_constexpr<T>::value, T>::type
  204. addressof(T& o) BOOST_NOEXCEPT
  205. {
  206. return &o;
  207. }
  208. } /* detail */
  209. template<class T>
  210. constexpr BOOST_FORCEINLINE T*
  211. addressof(T& o) BOOST_NOEXCEPT
  212. {
  213. return boost::detail::addressof(o);
  214. }
  215. #endif
  216. } /* boost */
  217. #endif
  218. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
  219. !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
  220. namespace boost {
  221. template<class T>
  222. const T* addressof(const T&&) = delete;
  223. } /* boost */
  224. #endif
  225. #endif