addressof.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___MEMORY_ADDRESSOF_H
  10. #define _LIBCPP___MEMORY_ADDRESSOF_H
  11. #include <__config>
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. # pragma GCC system_header
  14. #endif
  15. _LIBCPP_BEGIN_NAMESPACE_STD
  16. template <class _Tp>
  17. inline _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_NO_CFI _LIBCPP_HIDE_FROM_ABI _Tp* addressof(_Tp& __x) _NOEXCEPT {
  18. return __builtin_addressof(__x);
  19. }
  20. #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
  21. // Objective-C++ Automatic Reference Counting uses qualified pointers
  22. // that require special addressof() signatures. When
  23. // _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
  24. // itself is providing these definitions. Otherwise, we provide them.
  25. template <class _Tp>
  26. inline _LIBCPP_HIDE_FROM_ABI __strong _Tp* addressof(__strong _Tp& __x) _NOEXCEPT {
  27. return &__x;
  28. }
  29. # ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
  30. template <class _Tp>
  31. inline _LIBCPP_HIDE_FROM_ABI __weak _Tp* addressof(__weak _Tp& __x) _NOEXCEPT {
  32. return &__x;
  33. }
  34. # endif
  35. template <class _Tp>
  36. inline _LIBCPP_HIDE_FROM_ABI __autoreleasing _Tp* addressof(__autoreleasing _Tp& __x) _NOEXCEPT {
  37. return &__x;
  38. }
  39. template <class _Tp>
  40. inline _LIBCPP_HIDE_FROM_ABI __unsafe_unretained _Tp* addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT {
  41. return &__x;
  42. }
  43. #endif
  44. #if !defined(_LIBCPP_CXX03_LANG)
  45. template <class _Tp>
  46. _Tp* addressof(const _Tp&&) noexcept = delete;
  47. #endif
  48. _LIBCPP_END_NAMESPACE_STD
  49. #endif // _LIBCPP___MEMORY_ADDRESSOF_H