Skip to content

[ADT] Use adl_begin in make_first_range and make_second_range #130521

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

Merged
merged 1 commit into from
Mar 12, 2025
Merged
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
4 changes: 2 additions & 2 deletions llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ template <typename EltTy, typename FirstTy> class first_or_second_type {

/// Given a container of pairs, return a range over the first elements.
template <typename ContainerTy> auto make_first_range(ContainerTy &&c) {
using EltTy = decltype((*std::begin(c)));
using EltTy = decltype(*adl_begin(c));
return llvm::map_range(std::forward<ContainerTy>(c),
[](EltTy elt) -> typename detail::first_or_second_type<
EltTy, decltype((elt.first))>::type {
Expand All @@ -1444,7 +1444,7 @@ template <typename ContainerTy> auto make_first_range(ContainerTy &&c) {

/// Given a container of pairs, return a range over the second elements.
template <typename ContainerTy> auto make_second_range(ContainerTy &&c) {
using EltTy = decltype((*std::begin(c)));
using EltTy = decltype(*adl_begin(c));
return llvm::map_range(
std::forward<ContainerTy>(c),
[](EltTy elt) ->
Expand Down
19 changes: 18 additions & 1 deletion llvm/unittests/ADT/STLExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,17 @@ struct List {
std::list<int>::const_iterator begin(const List &list) {
return list.data.begin();
}

std::list<int>::const_iterator end(const List &list) { return list.data.end(); }

struct Pairs {
std::vector<std::pair<std::string, int>> data;
using const_iterator =
std::vector<std::pair<std::string, int>>::const_iterator;
};

Pairs::const_iterator begin(const Pairs &p) { return p.data.begin(); }
Pairs::const_iterator end(const Pairs &p) { return p.data.end(); }

struct requires_move {};
int *begin(requires_move &&) { return nullptr; }
int *end(requires_move &&) { return nullptr; }
Expand Down Expand Up @@ -524,6 +532,15 @@ TEST(STLExtrasTest, ConcatRangeADL) {
EXPECT_THAT(concat<const int>(S0, S1), ElementsAre(1, 2, 3, 4));
}

TEST(STLExtrasTest, MakeFirstSecondRangeADL) {
// Make sure that we use the `begin`/`end` functions from `some_namespace`,
// using ADL.
some_namespace::Pairs Pairs;
Pairs.data = {{"foo", 1}, {"bar", 2}};
EXPECT_THAT(make_first_range(Pairs), ElementsAre("foo", "bar"));
EXPECT_THAT(make_second_range(Pairs), ElementsAre(1, 2));
}

template <typename T> struct Iterator {
int i = 0;
T operator*() const { return i; }
Expand Down
Loading