File indexing completed on 2026-05-03 08:13:57
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___MEMORY_USES_ALLOCATOR_CONSTRUCTION_H
0010 #define _LIBCPP___MEMORY_USES_ALLOCATOR_CONSTRUCTION_H
0011
0012 #include <__config>
0013 #include <__memory/construct_at.h>
0014 #include <__memory/uses_allocator.h>
0015 #include <__tuple/tuple_like_no_subrange.h>
0016 #include <__type_traits/enable_if.h>
0017 #include <__type_traits/is_same.h>
0018 #include <__type_traits/remove_cv.h>
0019 #include <__utility/declval.h>
0020 #include <__utility/pair.h>
0021 #include <tuple>
0022
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 # pragma GCC system_header
0025 #endif
0026
0027 _LIBCPP_PUSH_MACROS
0028 #include <__undef_macros>
0029
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031
0032 #if _LIBCPP_STD_VER >= 17
0033
0034 template <class _Type>
0035 inline constexpr bool __is_std_pair = false;
0036
0037 template <class _Type1, class _Type2>
0038 inline constexpr bool __is_std_pair<pair<_Type1, _Type2>> = true;
0039
0040 template <class _Tp>
0041 inline constexpr bool __is_cv_std_pair = __is_std_pair<remove_cv_t<_Tp>>;
0042
0043 template <class _Tp, class = void>
0044 struct __uses_allocator_construction_args;
0045
0046 namespace __uses_allocator_detail {
0047
0048 template <class _Ap, class _Bp>
0049 void __fun(const pair<_Ap, _Bp>&);
0050
0051 template <class _Tp>
0052 decltype(__uses_allocator_detail::__fun(std::declval<_Tp>()), true_type()) __convertible_to_const_pair_ref_impl(int);
0053
0054 template <class>
0055 false_type __convertible_to_const_pair_ref_impl(...);
0056
0057 template <class _Tp>
0058 inline constexpr bool __convertible_to_const_pair_ref =
0059 decltype(__uses_allocator_detail::__convertible_to_const_pair_ref_impl<_Tp>(0))::value;
0060
0061 # if _LIBCPP_STD_VER >= 23
0062 template <class _Tp, class _Up>
0063 inline constexpr bool __uses_allocator_constraints =
0064 __is_cv_std_pair<_Tp> && !__pair_like_no_subrange<_Up> && !__convertible_to_const_pair_ref<_Up>;
0065 # else
0066 template <class _Tp, class _Up>
0067 inline constexpr bool __uses_allocator_constraints = __is_cv_std_pair<_Tp> && !__convertible_to_const_pair_ref<_Up>;
0068 # endif
0069
0070 }
0071
0072 template <class _Type, class _Alloc, class... _Args>
0073 _LIBCPP_HIDE_FROM_ABI constexpr _Type __make_obj_using_allocator(const _Alloc& __alloc, _Args&&... __args);
0074
0075 template <class _Pair>
0076 struct __uses_allocator_construction_args<_Pair, __enable_if_t<__is_cv_std_pair<_Pair>>> {
0077 template <class _Alloc, class _Tuple1, class _Tuple2>
0078 static _LIBCPP_HIDE_FROM_ABI constexpr auto
0079 __apply(const _Alloc& __alloc, piecewise_construct_t, _Tuple1&& __x, _Tuple2&& __y) noexcept {
0080 return std::make_tuple(
0081 piecewise_construct,
0082 std::apply(
0083 [&__alloc](auto&&... __args1) {
0084 return __uses_allocator_construction_args<typename _Pair::first_type>::__apply(
0085 __alloc, std::forward<decltype(__args1)>(__args1)...);
0086 },
0087 std::forward<_Tuple1>(__x)),
0088 std::apply(
0089 [&__alloc](auto&&... __args2) {
0090 return __uses_allocator_construction_args<typename _Pair::second_type>::__apply(
0091 __alloc, std::forward<decltype(__args2)>(__args2)...);
0092 },
0093 std::forward<_Tuple2>(__y)));
0094 }
0095
0096 template <class _Alloc>
0097 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc) noexcept {
0098 return __uses_allocator_construction_args<_Pair>::__apply(__alloc, piecewise_construct, tuple<>{}, tuple<>{});
0099 }
0100
0101 template <class _Alloc, class _Up, class _Vp>
0102 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, _Up&& __u, _Vp&& __v) noexcept {
0103 return __uses_allocator_construction_args<_Pair>::__apply(
0104 __alloc,
0105 piecewise_construct,
0106 std::forward_as_tuple(std::forward<_Up>(__u)),
0107 std::forward_as_tuple(std::forward<_Vp>(__v)));
0108 }
0109
0110 # if _LIBCPP_STD_VER >= 23
0111 template <class _Alloc, class _Up, class _Vp>
0112 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, pair<_Up, _Vp>& __pair) noexcept {
0113 return __uses_allocator_construction_args<_Pair>::__apply(
0114 __alloc, piecewise_construct, std::forward_as_tuple(__pair.first), std::forward_as_tuple(__pair.second));
0115 }
0116 # endif
0117
0118 template <class _Alloc, class _Up, class _Vp>
0119 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, const pair<_Up, _Vp>& __pair) noexcept {
0120 return __uses_allocator_construction_args<_Pair>::__apply(
0121 __alloc, piecewise_construct, std::forward_as_tuple(__pair.first), std::forward_as_tuple(__pair.second));
0122 }
0123
0124 template <class _Alloc, class _Up, class _Vp>
0125 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, pair<_Up, _Vp>&& __pair) noexcept {
0126 return __uses_allocator_construction_args<_Pair>::__apply(
0127 __alloc,
0128 piecewise_construct,
0129 std::forward_as_tuple(std::get<0>(std::move(__pair))),
0130 std::forward_as_tuple(std::get<1>(std::move(__pair))));
0131 }
0132
0133 # if _LIBCPP_STD_VER >= 23
0134 template <class _Alloc, class _Up, class _Vp>
0135 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, const pair<_Up, _Vp>&& __pair) noexcept {
0136 return __uses_allocator_construction_args<_Pair>::__apply(
0137 __alloc,
0138 piecewise_construct,
0139 std::forward_as_tuple(std::get<0>(std::move(__pair))),
0140 std::forward_as_tuple(std::get<1>(std::move(__pair))));
0141 }
0142
0143 template < class _Alloc, __pair_like_no_subrange _PairLike>
0144 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, _PairLike&& __p) noexcept {
0145 return __uses_allocator_construction_args<_Pair>::__apply(
0146 __alloc,
0147 piecewise_construct,
0148 std::forward_as_tuple(std::get<0>(std::forward<_PairLike>(__p))),
0149 std::forward_as_tuple(std::get<1>(std::forward<_PairLike>(__p))));
0150 }
0151 # endif
0152
0153 template <class _Alloc,
0154 class _Type,
0155 __enable_if_t<__uses_allocator_detail::__uses_allocator_constraints<_Pair, _Type>, int> = 0>
0156 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, _Type&& __value) noexcept {
0157 struct __pair_constructor {
0158 using _PairMutable = remove_cv_t<_Pair>;
0159
0160 _LIBCPP_HIDDEN constexpr auto __do_construct(const _PairMutable& __pair) const {
0161 return std::__make_obj_using_allocator<_PairMutable>(__alloc_, __pair);
0162 }
0163
0164 _LIBCPP_HIDDEN constexpr auto __do_construct(_PairMutable&& __pair) const {
0165 return std::__make_obj_using_allocator<_PairMutable>(__alloc_, std::move(__pair));
0166 }
0167
0168 const _Alloc& __alloc_;
0169 _Type& __value_;
0170
0171 _LIBCPP_HIDDEN constexpr operator _PairMutable() const { return __do_construct(std::forward<_Type>(__value_)); }
0172 };
0173
0174 return std::make_tuple(__pair_constructor{__alloc, __value});
0175 }
0176 };
0177
0178 template <class _Type>
0179 struct __uses_allocator_construction_args<_Type, __enable_if_t<!__is_cv_std_pair<_Type>>> {
0180 template <class _Alloc, class... _Args>
0181 static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, _Args&&... __args) noexcept {
0182 if constexpr (!uses_allocator_v<remove_cv_t<_Type>, _Alloc> && is_constructible_v<_Type, _Args...>) {
0183 return std::forward_as_tuple(std::forward<_Args>(__args)...);
0184 } else if constexpr (uses_allocator_v<remove_cv_t<_Type>, _Alloc> &&
0185 is_constructible_v<_Type, allocator_arg_t, const _Alloc&, _Args...>) {
0186 return tuple<allocator_arg_t, const _Alloc&, _Args&&...>(allocator_arg, __alloc, std::forward<_Args>(__args)...);
0187 } else if constexpr (uses_allocator_v<remove_cv_t<_Type>, _Alloc> &&
0188 is_constructible_v<_Type, _Args..., const _Alloc&>) {
0189 return std::forward_as_tuple(std::forward<_Args>(__args)..., __alloc);
0190 } else {
0191 static_assert(
0192 sizeof(_Type) + 1 == 0, "If uses_allocator_v<Type> is true, the type has to be allocator-constructible");
0193 }
0194 }
0195 };
0196
0197 template <class _Type, class _Alloc, class... _Args>
0198 _LIBCPP_HIDE_FROM_ABI constexpr _Type __make_obj_using_allocator(const _Alloc& __alloc, _Args&&... __args) {
0199 return std::make_from_tuple<_Type>(
0200 __uses_allocator_construction_args<_Type>::__apply(__alloc, std::forward<_Args>(__args)...));
0201 }
0202
0203 template <class _Type, class _Alloc, class... _Args>
0204 _LIBCPP_HIDE_FROM_ABI constexpr _Type*
0205 __uninitialized_construct_using_allocator(_Type* __ptr, const _Alloc& __alloc, _Args&&... __args) {
0206 return std::apply(
0207 [&__ptr](auto&&... __xs) { return std::__construct_at(__ptr, std::forward<decltype(__xs)>(__xs)...); },
0208 __uses_allocator_construction_args<_Type>::__apply(__alloc, std::forward<_Args>(__args)...));
0209 }
0210
0211 #endif
0212
0213 #if _LIBCPP_STD_VER >= 20
0214
0215 template <class _Type, class _Alloc, class... _Args>
0216 _LIBCPP_HIDE_FROM_ABI constexpr auto uses_allocator_construction_args(const _Alloc& __alloc, _Args&&... __args) noexcept
0217 -> decltype(__uses_allocator_construction_args<_Type>::__apply(__alloc, std::forward<_Args>(__args)...)) {
0218 return __uses_allocator_construction_args<_Type>::__apply(__alloc, std::forward<_Args>(__args)...);
0219 }
0220
0221 template <class _Type, class _Alloc, class... _Args>
0222 _LIBCPP_HIDE_FROM_ABI constexpr auto make_obj_using_allocator(const _Alloc& __alloc, _Args&&... __args)
0223 -> decltype(std::__make_obj_using_allocator<_Type>(__alloc, std::forward<_Args>(__args)...)) {
0224 return std::__make_obj_using_allocator<_Type>(__alloc, std::forward<_Args>(__args)...);
0225 }
0226
0227 template <class _Type, class _Alloc, class... _Args>
0228 _LIBCPP_HIDE_FROM_ABI constexpr auto
0229 uninitialized_construct_using_allocator(_Type* __ptr, const _Alloc& __alloc, _Args&&... __args)
0230 -> decltype(std::__uninitialized_construct_using_allocator(__ptr, __alloc, std::forward<_Args>(__args)...)) {
0231 return std::__uninitialized_construct_using_allocator(__ptr, __alloc, std::forward<_Args>(__args)...);
0232 }
0233
0234 #endif
0235
0236 _LIBCPP_END_NAMESPACE_STD
0237
0238 _LIBCPP_POP_MACROS
0239
0240 #endif