Skip to content

Commit b4a1872

Browse files
committed
[ADT] Use adl_being/end in `hasSingleElement
This is to make sure that ADT helpers consistently use argument dependent lookup when dealing with input ranges. This was a part of #87936 but reverted due to buildbot failures. Now that I have a threadripper system, I'm landing this piece-by-piece.
1 parent bd64f31 commit b4a1872

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ template <typename T> class Callable<T, true> {
320320

321321
/// Returns true if the given container only contains a single element.
322322
template <typename ContainerTy> bool hasSingleElement(ContainerTy &&C) {
323-
auto B = std::begin(C), E = std::end(C);
323+
auto B = adl_begin(C), E = adl_end(C);
324324
return B != E && std::next(B) == E;
325325
}
326326

llvm/unittests/ADT/STLExtrasTest.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,10 +934,19 @@ TEST(STLExtrasTest, hasSingleElement) {
934934
const std::vector<int> V0 = {}, V1 = {1}, V2 = {1, 2};
935935
const std::vector<int> V10(10);
936936

937-
EXPECT_EQ(hasSingleElement(V0), false);
938-
EXPECT_EQ(hasSingleElement(V1), true);
939-
EXPECT_EQ(hasSingleElement(V2), false);
940-
EXPECT_EQ(hasSingleElement(V10), false);
937+
EXPECT_FALSE(hasSingleElement(V0));
938+
EXPECT_TRUE(hasSingleElement(V1));
939+
EXPECT_FALSE(hasSingleElement(V2));
940+
EXPECT_FALSE(hasSingleElement(V10));
941+
942+
// Make sure that we use the `begin`/`end` functions
943+
// from `some_namespace`, using ADL.
944+
some_namespace::some_struct S;
945+
EXPECT_FALSE(hasSingleElement(S));
946+
S.data = V1;
947+
EXPECT_TRUE(hasSingleElement(S));
948+
S.data = V2;
949+
EXPECT_FALSE(hasSingleElement(S));
941950
}
942951

943952
TEST(STLExtrasTest, hasNItems) {

0 commit comments

Comments
 (0)