[libcxx-commits] [libcxx] 98e4fd0 - [libcxx][ranges] Fix `ranges::empty` when begin, end, and empty members are provided.

via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 13 10:08:07 PDT 2021


Author: zoecarver
Date: 2021-05-13T10:07:57-07:00
New Revision: 98e4fd0701d0c65229db876ce338c723231d8cb4

URL: https://github.com/llvm/llvm-project/commit/98e4fd0701d0c65229db876ce338c723231d8cb4
DIFF: https://github.com/llvm/llvm-project/commit/98e4fd0701d0c65229db876ce338c723231d8cb4.diff

LOG: [libcxx][ranges] Fix `ranges::empty` when begin, end, and empty members are provided.

Before this commit, we'd get a compilation error because the operator() overload was ambiguous.

Differential Revision: https://reviews.llvm.org/D102263

Added: 
    

Modified: 
    libcxx/include/__ranges/empty.h
    libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__ranges/empty.h b/libcxx/include/__ranges/empty.h
index 65f36cd1c6cb4..bc3f9c4559db6 100644
--- a/libcxx/include/__ranges/empty.h
+++ b/libcxx/include/__ranges/empty.h
@@ -41,6 +41,7 @@ namespace __empty {
 
   template <class _Tp>
   concept __can_compare_begin_end =
+    !__member_empty<_Tp> &&
     !__can_invoke_size<_Tp> &&
     requires(_Tp&& __t) {
       bool(ranges::begin(__t) == ranges::end(__t));

diff  --git a/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp b/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
index 6ea4b05799a52..e0013f67b221f 100644
--- a/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
+++ b/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
@@ -133,6 +133,18 @@ template<>
 inline constexpr bool std::disable_sized_range<DisabledSizeRangeWithBeginEnd> = true;
 static_assert(!std::is_invocable_v<RangeSizeT, DisabledSizeRangeWithBeginEnd&>);
 
+struct BeginEndAndEmpty {
+  int* begin();
+  int* end();
+  constexpr bool empty() { return true; }
+};
+
+struct BeginEndAndConstEmpty {
+  int* begin();
+  int* end();
+  constexpr bool empty() const { return true; }
+};
+
 constexpr bool testBeginEqualsEnd() {
   BeginEndNotSizedSentinel a;
   assert(std::ranges::empty(a) == true);
@@ -146,6 +158,13 @@ constexpr bool testBeginEqualsEnd() {
   DisabledSizeRangeWithBeginEnd d;
   assert(std::ranges::empty(d) == true);
 
+  BeginEndAndEmpty e;
+  assert(std::ranges::empty(e) == true);
+
+  BeginEndAndConstEmpty f;
+  assert(std::ranges::empty(f) == true);
+  assert(std::ranges::empty(std::as_const(f)) == true);
+
   return true;
 }
 


        


More information about the libcxx-commits mailing list