2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___MEMORY_CONSTRUCT_AT_H
11 #define _LIBCPP___MEMORY_CONSTRUCT_AT_H
15 #include <__iterator/access.h>
16 #include <__memory/addressof.h>
17 #include <__memory/voidify.h>
18 #include <__type_traits/enable_if.h>
19 #include <__type_traits/is_array.h>
20 #include <__utility/declval.h>
21 #include <__utility/forward.h>
22 #include <__utility/move.h>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
30 #include <__undef_macros>
32 _LIBCPP_BEGIN_NAMESPACE_STD
36 #if _LIBCPP_STD_VER >= 20
38 template <class _Tp
, class... _Args
, class = decltype(::new(std::declval
<void*>()) _Tp(std::declval
<_Args
>()...))>
39 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* construct_at(_Tp
* __location
, _Args
&&... __args
) {
40 _LIBCPP_ASSERT_UNCATEGORIZED(__location
!= nullptr, "null pointer given to construct_at");
41 return ::new (std::__voidify(*__location
)) _Tp(std::forward
<_Args
>(__args
)...);
46 template <class _Tp
, class... _Args
, class = decltype(::new(std::declval
<void*>()) _Tp(std::declval
<_Args
>()...))>
47 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp
* __construct_at(_Tp
* __location
, _Args
&&... __args
) {
48 #if _LIBCPP_STD_VER >= 20
49 return std::construct_at(__location
, std::forward
<_Args
>(__args
)...);
51 return _LIBCPP_ASSERT_UNCATEGORIZED(__location
!= nullptr, "null pointer given to construct_at"),
52 ::new (std::__voidify(*__location
)) _Tp(std::forward
<_Args
>(__args
)...);
58 // The internal functions are available regardless of the language version (with the exception of the `__destroy_at`
61 template <class _ForwardIterator
>
62 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
63 _ForwardIterator
__destroy(_ForwardIterator
, _ForwardIterator
);
65 template <class _Tp
, __enable_if_t
<!is_array
<_Tp
>::value
, int> = 0>
66 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
67 void __destroy_at(_Tp
* __loc
) {
68 _LIBCPP_ASSERT_UNCATEGORIZED(__loc
!= nullptr, "null pointer given to destroy_at");
72 #if _LIBCPP_STD_VER >= 20
73 template <class _Tp
, __enable_if_t
<is_array
<_Tp
>::value
, int> = 0>
74 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
75 void __destroy_at(_Tp
* __loc
) {
76 _LIBCPP_ASSERT_UNCATEGORIZED(__loc
!= nullptr, "null pointer given to destroy_at");
77 std::__destroy(std::begin(*__loc
), std::end(*__loc
));
81 template <class _ForwardIterator
>
82 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
83 _ForwardIterator
__destroy(_ForwardIterator __first
, _ForwardIterator __last
) {
84 for (; __first
!= __last
; ++__first
)
85 std::__destroy_at(std::addressof(*__first
));
89 template <class _BidirectionalIterator
>
90 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
91 _BidirectionalIterator
__reverse_destroy(_BidirectionalIterator __first
, _BidirectionalIterator __last
) {
92 while (__last
!= __first
) {
94 std::__destroy_at(std::addressof(*__last
));
99 #if _LIBCPP_STD_VER >= 17
101 template <class _Tp
, enable_if_t
<!is_array_v
<_Tp
>, int> = 0>
102 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
103 void destroy_at(_Tp
* __loc
) {
104 std::__destroy_at(__loc
);
107 #if _LIBCPP_STD_VER >= 20
108 template <class _Tp
, enable_if_t
<is_array_v
<_Tp
>, int> = 0>
109 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
110 void destroy_at(_Tp
* __loc
) {
111 std::__destroy_at(__loc
);
115 template <class _ForwardIterator
>
116 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
117 void destroy(_ForwardIterator __first
, _ForwardIterator __last
) {
118 (void)std::__destroy(std::move(__first
), std::move(__last
));
121 template <class _ForwardIterator
, class _Size
>
122 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
123 _ForwardIterator
destroy_n(_ForwardIterator __first
, _Size __n
) {
124 for (; __n
> 0; (void)++__first
, --__n
)
125 std::__destroy_at(std::addressof(*__first
));
129 #endif // _LIBCPP_STD_VER >= 17
131 _LIBCPP_END_NAMESPACE_STD
135 #endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H