addressof.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include <boost/atomic/detail/header.hpp>
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. #pragma once
  20. #endif
  21. // Detection logic is based on boost/core/addressof.hpp
  22. #if defined(BOOST_MSVC_FULL_VER) && BOOST_MSVC_FULL_VER >= 190024215
  23. #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF
  24. #elif defined(BOOST_GCC) && BOOST_GCC >= 70000
  25. #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF
  26. #elif defined(__has_builtin)
  27. #if __has_builtin(__builtin_addressof)
  28. #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF
  29. #endif
  30. #endif
  31. namespace boost {
  32. namespace atomics {
  33. namespace detail {
  34. template< typename T >
  35. BOOST_FORCEINLINE
  36. #if defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF)
  37. BOOST_CONSTEXPR
  38. #endif
  39. T* addressof(T& value) BOOST_NOEXCEPT
  40. {
  41. #if defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF)
  42. return __builtin_addressof(value);
  43. #else
  44. // Note: The point of using a local struct as the intermediate type instead of char is to avoid gcc warnings
  45. // if T is a const volatile char*:
  46. // warning: casting 'const volatile char* const' to 'const volatile char&' does not dereference pointer
  47. // The local struct makes sure T is not related to the cast target type.
  48. struct opaque_type;
  49. return reinterpret_cast< T* >(&const_cast< opaque_type& >(reinterpret_cast< const volatile opaque_type& >(value)));
  50. #endif
  51. }
  52. } // namespace detail
  53. } // namespace atomics
  54. } // namespace boost
  55. #include <boost/atomic/detail/footer.hpp>
  56. #endif // BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_