addressof.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2018 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/addressof.hpp
  10. *
  11. * This header defines \c addressof helper function. It is similar to \c boost::addressof but it is more
  12. * lightweight and also contains a workaround for some compiler warnings.
  13. */
  14. #ifndef BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_
  15. #define BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_
  16. #include <boost/atomic/detail/config.hpp>
  17. #ifdef BOOST_HAS_PRAGMA_ONCE
  18. #pragma once
  19. #endif
  20. // Detection logic is based on boost/core/addressof.hpp
  21. #if defined(BOOST_MSVC_FULL_VER) && BOOST_MSVC_FULL_VER >= 190024215
  22. #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF
  23. #elif defined(BOOST_GCC) && BOOST_GCC >= 70000
  24. #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF
  25. #elif defined(__has_builtin)
  26. #if __has_builtin(__builtin_addressof)
  27. #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF
  28. #endif
  29. #endif
  30. namespace boost {
  31. namespace atomics {
  32. namespace detail {
  33. template< typename T >
  34. BOOST_FORCEINLINE T* addressof(T& value) BOOST_NOEXCEPT
  35. {
  36. #if defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF)
  37. return __builtin_addressof(value);
  38. #else
  39. // Note: The point of using a local struct as the intermediate type instead of char is to avoid gcc warnings
  40. // if T is a const volatile char*:
  41. // warning: casting 'const volatile char* const' to 'const volatile char&' does not dereference pointer
  42. // The local struct makes sure T is not related to the cast target type.
  43. struct opaque_type;
  44. return reinterpret_cast< T* >(&const_cast< opaque_type& >(reinterpret_cast< const volatile opaque_type& >(value)));
  45. #endif
  46. }
  47. } // namespace detail
  48. } // namespace atomics
  49. } // namespace boost
  50. #endif // BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_