diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1513,19 +1513,21 @@ // Extra additions to //===----------------------------------------------------------------------===// -/// Function object to check whether the first component of a std::pair -/// compares less than the first component of another std::pair. +/// Function object to check whether the first component of a container +/// supported by std::get (like std::pair and std::tuple) compares less than the +/// first component of another container. struct less_first { template bool operator()(const T &lhs, const T &rhs) const { - return std::less<>()(lhs.first, rhs.first); + return std::less<>()(std::get<0>(lhs), std::get<0>(rhs)); } }; -/// Function object to check whether the second component of a std::pair -/// compares less than the second component of another std::pair. +/// Function object to check whether the second component of a container +/// supported by std::get (like std::pair and std::tuple) compares less than the +/// second component of another container. struct less_second { template bool operator()(const T &lhs, const T &rhs) const { - return std::less<>()(lhs.second, rhs.second); + return std::less<>()(std::get<1>(lhs), std::get<1>(rhs)); } }; diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include using namespace llvm; @@ -1055,4 +1057,36 @@ "addEnumValues(ULongLongMax, C::Two) failed."); } +TEST(STLExtrasTest, LessFirst) { + { + std::pair A(0, 1); + std::pair B(1, 0); + EXPECT_TRUE(less_first()(A, B)); + EXPECT_FALSE(less_first()(B, A)); + } + + { + std::tuple A(0, 1); + std::tuple B(1, 0); + EXPECT_TRUE(less_first()(A, B)); + EXPECT_FALSE(less_first()(B, A)); + } +} + +TEST(STLExtrasTest, LessSecond) { + { + std::pair A(0, 1); + std::pair B(1, 0); + EXPECT_FALSE(less_second()(A, B)); + EXPECT_TRUE(less_second()(B, A)); + } + + { + std::tuple A(0, 1); + std::tuple B(1, 0); + EXPECT_FALSE(less_second()(A, B)); + EXPECT_TRUE(less_second()(B, A)); + } +} + } // namespace