Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-3525: uses_allocator_construction_args fails to handle types convertible to pair #2639

Merged
merged 22 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions stl/inc/xmemory
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,30 @@ typename _Container::size_type _Erase_nodes_if(_Container& _Cont, _Pr _Pred) {
return _Old_size - _Cont.size();
}

template <class _Ty1, class _Ty2>
void _Deduce_as_pair(const pair<_Ty1, _Ty2>&);
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

template <class _Ty, class = void>
_INLINE_VAR constexpr bool _Cannot_as_pair = true;
frederick-vs-ja marked this conversation as resolved.
Show resolved Hide resolved

template <class _Ty>
_INLINE_VAR constexpr bool _Cannot_as_pair<_Ty, void_t<decltype(_STD _Deduce_as_pair(_STD declval<_Ty>()))>> = false;

template <class _Ty>
const _Ty& _Normally_bind(const _Ty&);

template <class _Ty>
_Ty&& _Normally_bind(_Ty&&);

template <class _Ty, class _Uty>
using _Normally_bound_ref = decltype(_STD _Normally_bind<_Ty>(_STD declval<_Uty>()));
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

template <class _Ty, class _Uty, class = void>
_INLINE_VAR constexpr bool _Is_normally_bindable = false;

template <class _Ty, class _Uty>
_INLINE_VAR constexpr bool _Is_normally_bindable<_Ty, _Uty, void_t<_Normally_bound_ref<_Ty, _Uty>>> = true;

#if _HAS_CXX20
template <class _Ty, class _Alloc, class... _Types, enable_if_t<!_Is_specialization_v<_Ty, pair>, int> = 0>
_NODISCARD constexpr auto uses_allocator_construction_args(const _Alloc& _Al, _Types&&... _Args) noexcept {
Expand Down Expand Up @@ -2098,6 +2122,10 @@ _NODISCARD constexpr auto uses_allocator_construction_args(const _Alloc& _Al, co
template <class _Ty, class _Alloc, class _Uty1, class _Uty2, enable_if_t<_Is_specialization_v<_Ty, pair>, int> = 0>
_NODISCARD constexpr auto uses_allocator_construction_args(const _Alloc& _Al, pair<_Uty1, _Uty2>&& _Pair) noexcept;

template <class _Ty, class _Alloc, class _Uty,
enable_if_t<_Is_specialization_v<_Ty, pair> && _Cannot_as_pair<_Uty&>, int> = 0>
_NODISCARD constexpr auto uses_allocator_construction_args(const _Alloc& _Al, _Uty&& _Ux) noexcept;

template <class _Ty, class _Alloc, class _Tuple1, class _Tuple2, enable_if_t<_Is_specialization_v<_Ty, pair>, int> = 0>
_NODISCARD constexpr auto uses_allocator_construction_args(
const _Alloc& _Al, piecewise_construct_t, _Tuple1&& _Tup1, _Tuple2&& _Tup2) noexcept {
Expand Down Expand Up @@ -2155,6 +2183,43 @@ _NODISCARD constexpr auto uses_allocator_construction_args(const _Alloc& _Al, pa
_STD uses_allocator_construction_args<typename _Ty::second_type>(_Al, _STD get<1>(_STD move(_Pair))));
}

template <class _Ty, class _Alloc, class _Uty,
enable_if_t<_Is_specialization_v<_Ty, pair> && _Cannot_as_pair<_Uty&>, int>>
_NODISCARD constexpr auto uses_allocator_construction_args(const _Alloc& _Al, _Uty&& _Ux) noexcept {
struct _Pair_remaker {
const _Alloc& _Al;
_Uty& _Ux;

constexpr operator remove_cv_t<_Ty>() const {
using _Pair_t = remove_cv_t<_Ty>;
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
static_assert(_Is_normally_bindable<_Pair_t, _Uty>,
"The argument must be bindable to a reference to the std::pair type.");

using _Pair_first_t = typename _Pair_t::first_type;
using _Pair_second_t = typename _Pair_t::second_type;
using _Pair_ref_t = _Normally_bound_ref<_Pair_t, _Uty>;
_Pair_ref_t _Pair_ref = _STD forward<_Uty>(_Ux);
if constexpr (is_same_v<_Pair_ref_t, const _Pair_t&>) {
// equivalent to
// return _STD make_obj_using_allocator<_Pair_t>(_Al, _Pair_ref);
return _Pair_t(piecewise_construct,
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
_STD uses_allocator_construction_args<_Pair_first_t>(_Al, _Pair_ref.first),
_STD uses_allocator_construction_args<_Pair_second_t>(_Al, _Pair_ref.second));
} else {
// equivalent to
// return _STD make_obj_using_allocator<_Pair_t>(_Al, _STD move(_Pair_ref));
return _Pair_t(piecewise_construct,
_STD uses_allocator_construction_args<_Pair_first_t>(_Al, _STD get<0>(_STD move(_Pair_ref))),
_STD uses_allocator_construction_args<_Pair_second_t>(_Al, _STD get<1>(_STD move(_Pair_ref))));
}
}
};

// equivalent to
// return _STD make_tuple(_Pair_remaker{_Al, _Ux});
return tuple<_Pair_remaker>({_Al, _Ux});
}

template <class _Ty, class _Alloc, class... _Types>
_NODISCARD constexpr _Ty make_obj_using_allocator(const _Alloc& _Al, _Types&&... _Args) {
return _STD make_from_tuple<_Ty>(_STD uses_allocator_construction_args<_Ty>(_Al, _STD forward<_Types>(_Args)...));
Expand Down
18 changes: 18 additions & 0 deletions stl/inc/xpolymorphic_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ void _Uses_allocator_construct(
_Uses_allocator_construct_pair(_Ptr, _Outer, _Inner, _STD forward_as_tuple(_STD forward<_Uty>(_Pair.first)),
_STD forward_as_tuple(_STD forward<_Vty>(_Pair.second)));
}

template <class _Ty1, class _Ty2, class _Outer_alloc, class _Inner_alloc, class _Uty,
enable_if_t<_Cannot_as_pair<_Uty>, int> = 0>
void _Uses_allocator_construct(pair<_Ty1, _Ty2>* const _Ptr, _Outer_alloc& _Outer, _Inner_alloc& _Inner, _Uty&& _Ux) {
// uses-allocator construction of pair by alloc _Outer propagating alloc _Inner, non-pair argument
static_assert(_Is_normally_bindable<pair<_Ty1, _Ty2>, _Uty>,
"The argument must be bindable to a reference to the std::pair type.");

using _Pair_ref_t = _Normally_bound_ref<pair<_Ty1, _Ty2>, _Uty>;
_Pair_ref_t _Pair_ref = _STD forward<_Uty>(_Ux);
if constexpr (is_same_v<_Pair_ref_t, const pair<_Ty1, _Ty2>&>) {
_Uses_allocator_construct_pair(
_Ptr, _Outer, _Inner, _STD forward_as_tuple(_Pair_ref.first), _STD forward_as_tuple(_Pair_ref.second));
} else {
_Uses_allocator_construct_pair(_Ptr, _Outer, _Inner, _STD forward_as_tuple(_STD forward<_Ty1>(_Pair_ref.first)),
_STD forward_as_tuple(_STD forward<_Ty2>(_Pair_ref.second)));
}
}
#endif // !_HAS_CXX20

#if _HAS_CXX17
Expand Down
31 changes: 31 additions & 0 deletions tests/std/tests/P0220R1_polymorphic_memory_resources/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,35 @@ namespace {
pmr_container_test<std::pmr::wsmatch>();
}
} // namespace containers

namespace map_containers {
template <class T>
void pair_conversion_test() {
struct pair_conv {
operator std::pair<const int, int>() const {
return {};
}
};

struct mem_pair_conv {
std::pair<const int, int> pair_{1, 42};
operator const std::pair<const int, int>&() const {
return pair_;
}
};

T cont;
cont.emplace(pair_conv{});
cont.emplace(mem_pair_conv{});
}

void test() {
pair_conversion_test<std::pmr::map<int, int>>();
pair_conversion_test<std::pmr::multimap<int, int>>();
pair_conversion_test<std::pmr::unordered_map<int, int>>();
pair_conversion_test<std::pmr::unordered_multimap<int, int>>();
}
} // namespace map_containers
} // unnamed namespace

int main() {
Expand Down Expand Up @@ -1491,4 +1520,6 @@ int main() {
pool::allocate_deallocate::test();

containers::test();

map_containers::test();
}