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 <__type_traits/enable_if.h>
18 #include <__type_traits/is_array.h>
19 #include <__utility/declval.h>
20 #include <__utility/forward.h>
21 #include <__utility/move.h>
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 # pragma GCC system_header
29 #include <__undef_macros>
31 _LIBCPP_BEGIN_NAMESPACE_STD
35 #if _LIBCPP_STD_VER >= 20
37 template <class _Tp
, class... _Args
, class = decltype(::new(std::declval
<void*>()) _Tp(std::declval
<_Args
>()...))>
38 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* construct_at(_Tp
* __location
, _Args
&&... __args
) {
39 _LIBCPP_ASSERT_NON_NULL(__location
!= nullptr, "null pointer given to construct_at");
40 return ::new (static_cast<void*>(__location
)) _Tp(std::forward
<_Args
>(__args
)...);
45 template <class _Tp
, class... _Args
, class = decltype(::new(std::declval
<void*>()) _Tp(std::declval
<_Args
>()...))>
46 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
* __construct_at(_Tp
* __location
, _Args
&&... __args
) {
47 #if _LIBCPP_STD_VER >= 20
48 return std::construct_at(__location
, std::forward
<_Args
>(__args
)...);
50 return _LIBCPP_ASSERT_NON_NULL(__location
!= nullptr, "null pointer given to construct_at"),
51 ::new (static_cast<void*>(__location
)) _Tp(std::forward
<_Args
>(__args
)...);
57 // The internal functions are available regardless of the language version (with the exception of the `__destroy_at`
60 template <class _ForwardIterator
>
61 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
__destroy(_ForwardIterator
, _ForwardIterator
);
63 template <class _Tp
, __enable_if_t
<!is_array
<_Tp
>::value
, int> = 0>
64 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
void __destroy_at(_Tp
* __loc
) {
65 _LIBCPP_ASSERT_NON_NULL(__loc
!= nullptr, "null pointer given to destroy_at");
69 #if _LIBCPP_STD_VER >= 20
70 template <class _Tp
, __enable_if_t
<is_array
<_Tp
>::value
, int> = 0>
71 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
void __destroy_at(_Tp
* __loc
) {
72 _LIBCPP_ASSERT_NON_NULL(__loc
!= nullptr, "null pointer given to destroy_at");
73 std::__destroy(std::begin(*__loc
), std::end(*__loc
));
77 template <class _ForwardIterator
>
78 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
79 __destroy(_ForwardIterator __first
, _ForwardIterator __last
) {
80 for (; __first
!= __last
; ++__first
)
81 std::__destroy_at(std::addressof(*__first
));
85 template <class _BidirectionalIterator
>
86 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _BidirectionalIterator
87 __reverse_destroy(_BidirectionalIterator __first
, _BidirectionalIterator __last
) {
88 while (__last
!= __first
) {
90 std::__destroy_at(std::addressof(*__last
));
95 #if _LIBCPP_STD_VER >= 17
97 template <class _Tp
, enable_if_t
<!is_array_v
<_Tp
>, int> = 0>
98 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
void destroy_at(_Tp
* __loc
) {
99 std::__destroy_at(__loc
);
102 # if _LIBCPP_STD_VER >= 20
103 template <class _Tp
, enable_if_t
<is_array_v
<_Tp
>, int> = 0>
104 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
void destroy_at(_Tp
* __loc
) {
105 std::__destroy_at(__loc
);
109 template <class _ForwardIterator
>
110 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
void destroy(_ForwardIterator __first
, _ForwardIterator __last
) {
111 (void)std::__destroy(std::move(__first
), std::move(__last
));
114 template <class _ForwardIterator
, class _Size
>
115 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
destroy_n(_ForwardIterator __first
, _Size __n
) {
116 for (; __n
> 0; (void)++__first
, --__n
)
117 std::__destroy_at(std::addressof(*__first
));
121 #endif // _LIBCPP_STD_VER >= 17
123 _LIBCPP_END_NAMESPACE_STD
127 #endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H