diff --git a/libcxx/include/__memory/uses_allocator_construction.h b/libcxx/include/__memory/uses_allocator_construction.h index 5e5819d4c281e..955879ffc5845 100644 --- a/libcxx/include/__memory/uses_allocator_construction.h +++ b/libcxx/include/__memory/uses_allocator_construction.h @@ -40,104 +40,8 @@ inline constexpr bool __is_std_pair> = true; template inline constexpr bool __is_cv_std_pair = __is_std_pair>; -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, _Args&&... __args) noexcept { - if constexpr (!uses_allocator_v, _Alloc> && is_constructible_v<_Type, _Args...>) { - return std::forward_as_tuple(std::forward<_Args>(__args)...); - } else if constexpr (uses_allocator_v, _Alloc> && - is_constructible_v<_Type, allocator_arg_t, const _Alloc&, _Args...>) { - return tuple(allocator_arg, __alloc, std::forward<_Args>(__args)...); - } else if constexpr (uses_allocator_v, _Alloc> && - is_constructible_v<_Type, _Args..., const _Alloc&>) { - return std::forward_as_tuple(std::forward<_Args>(__args)..., __alloc); - } else { - static_assert( - sizeof(_Type) + 1 == 0, "If uses_allocator_v is true, the type has to be allocator-constructible"); - } -} - -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto __uses_allocator_construction_args( - const _Alloc& __alloc, piecewise_construct_t, _Tuple1&& __x, _Tuple2&& __y) noexcept { - return std::make_tuple( - piecewise_construct, - std::apply( - [&__alloc](auto&&... __args1) { - return std::__uses_allocator_construction_args( - __alloc, std::forward(__args1)...); - }, - std::forward<_Tuple1>(__x)), - std::apply( - [&__alloc](auto&&... __args2) { - return std::__uses_allocator_construction_args( - __alloc, std::forward(__args2)...); - }, - std::forward<_Tuple2>(__y))); -} - -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto __uses_allocator_construction_args(const _Alloc& __alloc) noexcept { - return std::__uses_allocator_construction_args<_Pair>(__alloc, piecewise_construct, tuple<>{}, tuple<>{}); -} - -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, _Up&& __u, _Vp&& __v) noexcept { - return std::__uses_allocator_construction_args<_Pair>( - __alloc, - piecewise_construct, - std::forward_as_tuple(std::forward<_Up>(__u)), - std::forward_as_tuple(std::forward<_Vp>(__v))); -} - -# if _LIBCPP_STD_VER >= 23 -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, pair<_Up, _Vp>& __pair) noexcept { - return std::__uses_allocator_construction_args<_Pair>( - __alloc, piecewise_construct, std::forward_as_tuple(__pair.first), std::forward_as_tuple(__pair.second)); -} -# endif - -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, const pair<_Up, _Vp>& __pair) noexcept { - return std::__uses_allocator_construction_args<_Pair>( - __alloc, piecewise_construct, std::forward_as_tuple(__pair.first), std::forward_as_tuple(__pair.second)); -} - -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, pair<_Up, _Vp>&& __pair) noexcept { - return std::__uses_allocator_construction_args<_Pair>( - __alloc, - piecewise_construct, - std::forward_as_tuple(std::get<0>(std::move(__pair))), - std::forward_as_tuple(std::get<1>(std::move(__pair)))); -} - -# if _LIBCPP_STD_VER >= 23 -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, const pair<_Up, _Vp>&& __pair) noexcept { - return std::__uses_allocator_construction_args<_Pair>( - __alloc, - piecewise_construct, - std::forward_as_tuple(std::get<0>(std::move(__pair))), - std::forward_as_tuple(std::get<1>(std::move(__pair)))); -} - -template , int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, _PairLike&& __p) noexcept { - return std::__uses_allocator_construction_args<_Pair>( - __alloc, - piecewise_construct, - std::forward_as_tuple(std::get<0>(std::forward<_PairLike>(__p))), - std::forward_as_tuple(std::get<1>(std::forward<_PairLike>(__p)))); -} -# endif +template +struct __uses_allocator_construction_args; namespace __uses_allocator_detail { @@ -165,46 +69,135 @@ inline constexpr bool __uses_allocator_constraints = __is_cv_std_pair<_Tp> && !_ } // namespace __uses_allocator_detail -template < class _Pair, - class _Alloc, - class _Type, - __enable_if_t<__uses_allocator_detail::__uses_allocator_constraints<_Pair, _Type>, int> = 0> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, _Type&& __value) noexcept; - template _LIBCPP_HIDE_FROM_ABI constexpr _Type __make_obj_using_allocator(const _Alloc& __alloc, _Args&&... __args); -template < class _Pair, - class _Alloc, - class _Type, - __enable_if_t< __uses_allocator_detail::__uses_allocator_constraints<_Pair, _Type>, int>> -_LIBCPP_HIDE_FROM_ABI constexpr auto -__uses_allocator_construction_args(const _Alloc& __alloc, _Type&& __value) noexcept { - struct __pair_constructor { - using _PairMutable = remove_cv_t<_Pair>; +template +struct __uses_allocator_construction_args<_Pair, __enable_if_t<__is_cv_std_pair<_Pair>>> { + template + static _LIBCPP_HIDE_FROM_ABI constexpr auto + __apply(const _Alloc& __alloc, piecewise_construct_t, _Tuple1&& __x, _Tuple2&& __y) noexcept { + return std::make_tuple( + piecewise_construct, + std::apply( + [&__alloc](auto&&... __args1) { + return __uses_allocator_construction_args::__apply( + __alloc, std::forward(__args1)...); + }, + std::forward<_Tuple1>(__x)), + std::apply( + [&__alloc](auto&&... __args2) { + return __uses_allocator_construction_args::__apply( + __alloc, std::forward(__args2)...); + }, + std::forward<_Tuple2>(__y))); + } - _LIBCPP_HIDDEN constexpr auto __do_construct(const _PairMutable& __pair) const { - return std::__make_obj_using_allocator<_PairMutable>(__alloc_, __pair); - } + template + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc) noexcept { + return __uses_allocator_construction_args<_Pair>::__apply(__alloc, piecewise_construct, tuple<>{}, tuple<>{}); + } - _LIBCPP_HIDDEN constexpr auto __do_construct(_PairMutable&& __pair) const { - return std::__make_obj_using_allocator<_PairMutable>(__alloc_, std::move(__pair)); - } + template + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, _Up&& __u, _Vp&& __v) noexcept { + return __uses_allocator_construction_args<_Pair>::__apply( + __alloc, + piecewise_construct, + std::forward_as_tuple(std::forward<_Up>(__u)), + std::forward_as_tuple(std::forward<_Vp>(__v))); + } - const _Alloc& __alloc_; - _Type& __value_; +# if _LIBCPP_STD_VER >= 23 + template + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, pair<_Up, _Vp>& __pair) noexcept { + return __uses_allocator_construction_args<_Pair>::__apply( + __alloc, piecewise_construct, std::forward_as_tuple(__pair.first), std::forward_as_tuple(__pair.second)); + } +# endif - _LIBCPP_HIDDEN constexpr operator _PairMutable() const { return __do_construct(std::forward<_Type>(__value_)); } - }; + template + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, const pair<_Up, _Vp>& __pair) noexcept { + return __uses_allocator_construction_args<_Pair>::__apply( + __alloc, piecewise_construct, std::forward_as_tuple(__pair.first), std::forward_as_tuple(__pair.second)); + } - return std::make_tuple(__pair_constructor{__alloc, __value}); -} + template + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, pair<_Up, _Vp>&& __pair) noexcept { + return __uses_allocator_construction_args<_Pair>::__apply( + __alloc, + piecewise_construct, + std::forward_as_tuple(std::get<0>(std::move(__pair))), + std::forward_as_tuple(std::get<1>(std::move(__pair)))); + } + +# if _LIBCPP_STD_VER >= 23 + template + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, const pair<_Up, _Vp>&& __pair) noexcept { + return __uses_allocator_construction_args<_Pair>::__apply( + __alloc, + piecewise_construct, + std::forward_as_tuple(std::get<0>(std::move(__pair))), + std::forward_as_tuple(std::get<1>(std::move(__pair)))); + } + + template < class _Alloc, __pair_like_no_subrange _PairLike> + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, _PairLike&& __p) noexcept { + return __uses_allocator_construction_args<_Pair>::__apply( + __alloc, + piecewise_construct, + std::forward_as_tuple(std::get<0>(std::forward<_PairLike>(__p))), + std::forward_as_tuple(std::get<1>(std::forward<_PairLike>(__p)))); + } +# endif + + template , int> = 0> + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, _Type&& __value) noexcept { + struct __pair_constructor { + using _PairMutable = remove_cv_t<_Pair>; + + _LIBCPP_HIDDEN constexpr auto __do_construct(const _PairMutable& __pair) const { + return std::__make_obj_using_allocator<_PairMutable>(__alloc_, __pair); + } + + _LIBCPP_HIDDEN constexpr auto __do_construct(_PairMutable&& __pair) const { + return std::__make_obj_using_allocator<_PairMutable>(__alloc_, std::move(__pair)); + } + + const _Alloc& __alloc_; + _Type& __value_; + + _LIBCPP_HIDDEN constexpr operator _PairMutable() const { return __do_construct(std::forward<_Type>(__value_)); } + }; + + return std::make_tuple(__pair_constructor{__alloc, __value}); + } +}; + +template +struct __uses_allocator_construction_args<_Type, __enable_if_t>> { + template + static _LIBCPP_HIDE_FROM_ABI constexpr auto __apply(const _Alloc& __alloc, _Args&&... __args) noexcept { + if constexpr (!uses_allocator_v, _Alloc> && is_constructible_v<_Type, _Args...>) { + return std::forward_as_tuple(std::forward<_Args>(__args)...); + } else if constexpr (uses_allocator_v, _Alloc> && + is_constructible_v<_Type, allocator_arg_t, const _Alloc&, _Args...>) { + return tuple(allocator_arg, __alloc, std::forward<_Args>(__args)...); + } else if constexpr (uses_allocator_v, _Alloc> && + is_constructible_v<_Type, _Args..., const _Alloc&>) { + return std::forward_as_tuple(std::forward<_Args>(__args)..., __alloc); + } else { + static_assert( + sizeof(_Type) + 1 == 0, "If uses_allocator_v is true, the type has to be allocator-constructible"); + } + } +}; template _LIBCPP_HIDE_FROM_ABI constexpr _Type __make_obj_using_allocator(const _Alloc& __alloc, _Args&&... __args) { return std::make_from_tuple<_Type>( - std::__uses_allocator_construction_args<_Type>(__alloc, std::forward<_Args>(__args)...)); + __uses_allocator_construction_args<_Type>::__apply(__alloc, std::forward<_Args>(__args)...)); } template @@ -212,7 +205,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Type* __uninitialized_construct_using_allocator(_Type* __ptr, const _Alloc& __alloc, _Args&&... __args) { return std::apply( [&__ptr](auto&&... __xs) { return std::__construct_at(__ptr, std::forward(__xs)...); }, - std::__uses_allocator_construction_args<_Type>(__alloc, std::forward<_Args>(__args)...)); + __uses_allocator_construction_args<_Type>::__apply(__alloc, std::forward<_Args>(__args)...)); } #endif // _LIBCPP_STD_VER >= 17 @@ -221,8 +214,8 @@ __uninitialized_construct_using_allocator(_Type* __ptr, const _Alloc& __alloc, _ template _LIBCPP_HIDE_FROM_ABI constexpr auto uses_allocator_construction_args(const _Alloc& __alloc, _Args&&... __args) noexcept - -> decltype(std::__uses_allocator_construction_args<_Type>(__alloc, std::forward<_Args>(__args)...)) { - return /*--*/ std::__uses_allocator_construction_args<_Type>(__alloc, std::forward<_Args>(__args)...); + -> decltype(__uses_allocator_construction_args<_Type>::__apply(__alloc, std::forward<_Args>(__args)...)) { + return /*--*/ __uses_allocator_construction_args<_Type>::__apply(__alloc, std::forward<_Args>(__args)...); } template diff --git a/libcxx/test/std/utilities/memory/allocator.uses/allocator.uses.construction/uses_allocator_construction_args.pass.cpp b/libcxx/test/std/utilities/memory/allocator.uses/allocator.uses.construction/uses_allocator_construction_args.pass.cpp index ab3c039497dd9..235a93987fe48 100644 --- a/libcxx/test/std/utilities/memory/allocator.uses/allocator.uses.construction/uses_allocator_construction_args.pass.cpp +++ b/libcxx/test/std/utilities/memory/allocator.uses/allocator.uses.construction/uses_allocator_construction_args.pass.cpp @@ -121,6 +121,63 @@ constexpr void testOne() { assert(std::get<2>(std::get<1>(ret)) == 3); assert(std::get<0>(std::get<2>(ret)) == 4); } + { + // Tests for ensuring forward declarations of uses_allocator_construction_args + // See https://github.com/llvm/llvm-project/issues/66714. + { + using NestedPairType = std::pair>; + std::same_as, + std::tuple, std::tuple>>> auto + ret = test_uses_allocator_construction_args(a); + (void)ret; + } + { + using NestedPairType = std::pair>; + std::same_as, + std::tuple, std::tuple<>>>> auto + ret = test_uses_allocator_construction_args(a); + (void)ret; + } + { + using PairType = std::pair; + int up = 1; + int vp = 2; + + std::same_as, std::tuple>> auto ret = + test_uses_allocator_construction_args(a, std::move(up), std::move(vp)); + (void)ret; + } + { + using PairType = const std::pair; + PairType p(1, 2); + + std::same_as, std::tuple>> auto ret = + test_uses_allocator_construction_args(a, p); + (void)ret; + } + { + using PairType = std::pair; + PairType p(1, 2); + + std::same_as, std::tuple>> auto ret = + test_uses_allocator_construction_args(a, std::move(p)); + (void)ret; + } +#if TEST_STD_VER >= 23 + { + using PairType = const std::pair; + PairType p(1, 2); + + std::same_as, std::tuple>> auto ret = + test_uses_allocator_construction_args(a, std::move(p)); + (void)ret; + } +#endif + } #if TEST_STD_VER >= 23 { std::pair p{3, 4};