xref: /llvm-project/libcxx/include/__memory/construct_at.h (revision 9474e09459189fbed30f329a669f9c14979c5367)
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 
10 #ifndef _LIBCPP___MEMORY_CONSTRUCT_AT_H
11 #define _LIBCPP___MEMORY_CONSTRUCT_AT_H
12 
13 #include <__assert>
14 #include <__config>
15 #include <__iterator/access.h>
16 #include <__memory/addressof.h>
17 #include <__new/placement_new_delete.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>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #  pragma GCC system_header
26 #endif
27 
28 _LIBCPP_PUSH_MACROS
29 #include <__undef_macros>
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 // construct_at
34 
35 #if _LIBCPP_STD_VER >= 20
36 
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)...);
41 }
42 
43 #endif
44 
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)...);
49 #else
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)...);
52 #endif
53 }
54 
55 // destroy_at
56 
57 // The internal functions are available regardless of the language version (with the exception of the `__destroy_at`
58 // taking an array).
59 
60 template <class _ForwardIterator>
61 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator __destroy(_ForwardIterator, _ForwardIterator);
62 
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");
66   __loc->~_Tp();
67 }
68 
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));
74 }
75 #endif
76 
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));
82   return __first;
83 }
84 
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) {
89     --__last;
90     std::__destroy_at(std::addressof(*__last));
91   }
92   return __last;
93 }
94 
95 #if _LIBCPP_STD_VER >= 17
96 
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);
100 }
101 
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);
106 }
107 #  endif
108 
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));
112 }
113 
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));
118   return __first;
119 }
120 
121 #endif // _LIBCPP_STD_VER >= 17
122 
123 _LIBCPP_END_NAMESPACE_STD
124 
125 _LIBCPP_POP_MACROS
126 
127 #endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H
128