File indexing completed on 2026-05-03 08:13:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___MEMORY_CONSTRUCT_AT_H
0011 #define _LIBCPP___MEMORY_CONSTRUCT_AT_H
0012
0013 #include <__assert>
0014 #include <__config>
0015 #include <__iterator/access.h>
0016 #include <__memory/addressof.h>
0017 #include <__new/placement_new_delete.h>
0018 #include <__type_traits/enable_if.h>
0019 #include <__type_traits/is_array.h>
0020 #include <__utility/declval.h>
0021 #include <__utility/forward.h>
0022 #include <__utility/move.h>
0023
0024 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 # pragma GCC system_header
0026 #endif
0027
0028 _LIBCPP_PUSH_MACROS
0029 #include <__undef_macros>
0030
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032
0033
0034
0035 #if _LIBCPP_STD_VER >= 20
0036
0037 template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
0038 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) {
0039 _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at");
0040 return ::new (static_cast<void*>(__location)) _Tp(std::forward<_Args>(__args)...);
0041 }
0042
0043 #endif
0044
0045 template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
0046 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* __construct_at(_Tp* __location, _Args&&... __args) {
0047 #if _LIBCPP_STD_VER >= 20
0048 return std::construct_at(__location, std::forward<_Args>(__args)...);
0049 #else
0050 return _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at"),
0051 ::new (static_cast<void*>(__location)) _Tp(std::forward<_Args>(__args)...);
0052 #endif
0053 }
0054
0055
0056
0057
0058
0059
0060 template <class _ForwardIterator>
0061 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator __destroy(_ForwardIterator, _ForwardIterator);
0062
0063 template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
0064 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
0065 _LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
0066 __loc->~_Tp();
0067 }
0068
0069 #if _LIBCPP_STD_VER >= 20
0070 template <class _Tp, __enable_if_t<is_array<_Tp>::value, int> = 0>
0071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
0072 _LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
0073 std::__destroy(std::begin(*__loc), std::end(*__loc));
0074 }
0075 #endif
0076
0077 template <class _ForwardIterator>
0078 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
0079 __destroy(_ForwardIterator __first, _ForwardIterator __last) {
0080 for (; __first != __last; ++__first)
0081 std::__destroy_at(std::addressof(*__first));
0082 return __first;
0083 }
0084
0085 template <class _BidirectionalIterator>
0086 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
0087 __reverse_destroy(_BidirectionalIterator __first, _BidirectionalIterator __last) {
0088 while (__last != __first) {
0089 --__last;
0090 std::__destroy_at(std::addressof(*__last));
0091 }
0092 return __last;
0093 }
0094
0095 #if _LIBCPP_STD_VER >= 17
0096
0097 template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
0098 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {
0099 std::__destroy_at(__loc);
0100 }
0101
0102 # if _LIBCPP_STD_VER >= 20
0103 template <class _Tp, enable_if_t<is_array_v<_Tp>, int> = 0>
0104 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {
0105 std::__destroy_at(__loc);
0106 }
0107 # endif
0108
0109 template <class _ForwardIterator>
0110 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy(_ForwardIterator __first, _ForwardIterator __last) {
0111 (void)std::__destroy(std::move(__first), std::move(__last));
0112 }
0113
0114 template <class _ForwardIterator, class _Size>
0115 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
0116 for (; __n > 0; (void)++__first, --__n)
0117 std::__destroy_at(std::addressof(*__first));
0118 return __first;
0119 }
0120
0121 #endif
0122
0123 _LIBCPP_END_NAMESPACE_STD
0124
0125 _LIBCPP_POP_MACROS
0126
0127 #endif