Skip to content

[libc++][ranges] Fix ranges::to with ADL-only begin/end #119161

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 16 additions & 14 deletions libcxx/include/__ranges/to.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef _LIBCPP___RANGES_TO_H
#define _LIBCPP___RANGES_TO_H

#include <__algorithm/ranges_for_each.h>
#include <__concepts/constructible.h>
#include <__concepts/convertible_to.h>
#include <__concepts/derived_from.h>
Expand Down Expand Up @@ -70,6 +71,20 @@ concept __constructible_from_iter_pair =
derived_from<typename iterator_traits<iterator_t<_Range>>::iterator_category, input_iterator_tag> &&
constructible_from<_Container, iterator_t<_Range>, sentinel_t<_Range>, _Args...>;

template <class _Ref, class _Container>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto __container_append(_Container& __c) {
if constexpr (requires { __c.emplace_back(std::declval<_Ref>()); })
return [&__c](_Ref&& __ref) { __c.emplace_back(std::forward<_Ref>(__ref)); };
else if constexpr (requires { __c.push_back(std::declval<_Ref>()); })
return [&__c](_Ref&& __ref) { __c.push_back(std::forward<_Ref>(__ref)); };
else if constexpr (requires { __c.emplace(__c.end(), std::declval<_Ref>()); })
return [&__c](_Ref&& __ref) { __c.emplace(__c.end(), std::forward<_Ref>(__ref)); };
else {
static_assert(requires { __c.insert(__c.end(), std::declval<_Ref>()); });
return [&__c](_Ref&& __ref) { __c.insert(__c.end(), std::forward<_Ref>(__ref)); };
}
}

template <class>
concept __always_false = false;

Expand Down Expand Up @@ -109,21 +124,8 @@ template <class _Container, input_range _Range, class... _Args>
__result.reserve(static_cast<range_size_t<_Container>>(ranges::size(__range)));
}

for (auto&& __ref : __range) {
using _Ref = decltype(__ref);
if constexpr (requires { __result.emplace_back(std::declval<_Ref>()); }) {
__result.emplace_back(std::forward<_Ref>(__ref));
} else if constexpr (requires { __result.push_back(std::declval<_Ref>()); }) {
__result.push_back(std::forward<_Ref>(__ref));
} else if constexpr (requires { __result.emplace(__result.end(), std::declval<_Ref>()); }) {
__result.emplace(__result.end(), std::forward<_Ref>(__ref));
} else {
static_assert(requires { __result.insert(__result.end(), std::declval<_Ref>()); });
__result.insert(__result.end(), std::forward<_Ref>(__ref));
}
}
ranges::for_each(__range, ranges::__container_append<ranges::range_reference_t<_Range>>(__result));
return __result;

} else {
static_assert(__always_false<_Container>, "ranges::to: unable to convert to the given container type.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,30 @@ constexpr void test_recursive() {
assert(std::ranges::to<C4>(in_owning_view) == result);
}

struct adl_only_range {
static constexpr int numbers[2]{42, 1729};

void begin() const = delete;
void end() const = delete;

friend constexpr const int* begin(const adl_only_range&) { return std::ranges::begin(numbers); }
friend constexpr const int* end(const adl_only_range&) { return std::ranges::end(numbers); }
};

constexpr void test_lwg4016_regression() {
using Cont = Container<int, CtrChoice::DefaultCtrAndInsert, InserterChoice::PushBack, true>;

std::ranges::contiguous_range auto r = adl_only_range{};
auto v = r | std::ranges::to<Cont>();
assert(std::ranges::equal(v, adl_only_range::numbers));
}

constexpr bool test() {
test_constraints();
test_ctr_choice_order();
test_lwg_3785();
test_recursive();
test_lwg4016_regression();

return true;
}
Expand Down
Loading