Skip to content
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
3 changes: 2 additions & 1 deletion llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ template <typename T> class Callable<T, true> {

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

Expand Down
17 changes: 13 additions & 4 deletions llvm/unittests/ADT/STLExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,19 @@ TEST(STLExtrasTest, hasSingleElement) {
const std::vector<int> V0 = {}, V1 = {1}, V2 = {1, 2};
const std::vector<int> V10(10);

EXPECT_EQ(hasSingleElement(V0), false);
EXPECT_EQ(hasSingleElement(V1), true);
EXPECT_EQ(hasSingleElement(V2), false);
EXPECT_EQ(hasSingleElement(V10), false);
EXPECT_FALSE(hasSingleElement(V0));
EXPECT_TRUE(hasSingleElement(V1));
EXPECT_FALSE(hasSingleElement(V2));
EXPECT_FALSE(hasSingleElement(V10));

// Make sure that we use the `begin`/`end` functions
// from `some_namespace`, using ADL.
some_namespace::some_struct S;
EXPECT_FALSE(hasSingleElement(S));
S.data = V1;
EXPECT_TRUE(hasSingleElement(S));
S.data = V2;
EXPECT_FALSE(hasSingleElement(S));
}

TEST(STLExtrasTest, hasNItems) {
Expand Down
Loading