[libcxx-commits] [PATCH] D116808: fix __simple_view concept in std::ranges

Hui via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 7 13:01:46 PST 2022


huixie90 updated this revision to Diff 398222.
huixie90 added a comment.

Updating D116808 <https://reviews.llvm.org/D116808>: fix __simple_view concept in std::ranges

added unit test for take_view


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116808/new/

https://reviews.llvm.org/D116808

Files:
  libcxx/include/__ranges/concepts.h
  libcxx/test/libcxx/ranges/range.utility.helpers/simple_view.compile.pass.cpp
  libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp


Index: libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
===================================================================
--- libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
+++ libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
@@ -21,6 +21,18 @@
 #include "test_range.h"
 #include "types.h"
 
+namespace {
+
+struct NonCommonSimpleView : std::ranges::view_base {
+  int* begin() const;
+  sentinel_wrapper<int*> end() const;
+
+  // non const so that size_range<const DifferentSentinel> is false
+  size_t size();
+};
+
+} // namespace
+
 constexpr bool test() {
   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
 
@@ -63,6 +75,14 @@
     ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator<int*>);
   }
 
+  // __simple_view<V> && sized_range<V> && !size_range<!V>
+  {
+    std::ranges::take_view<NonCommonSimpleView> tv(NonCommonSimpleView{}, 4);
+    ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator<int*>);
+    // the optimal result should be int* but according to the c++20 standard,
+    // it returns std::counted_iterator<int*>
+  }
+
   return true;
 }
 
Index: libcxx/test/libcxx/ranges/range.utility.helpers/simple_view.compile.pass.cpp
===================================================================
--- libcxx/test/libcxx/ranges/range.utility.helpers/simple_view.compile.pass.cpp
+++ libcxx/test/libcxx/ranges/range.utility.helpers/simple_view.compile.pass.cpp
@@ -39,7 +39,14 @@
   sentinel_wrapper<int*> end() const;
 };
 
+struct WrongConstSentinel : std::ranges::view_base {
+  int *begin() const;
+  sentinel_wrapper<int*> end();
+  sentinel_wrapper<const int*> end() const;
+};
+
 static_assert( std::ranges::__simple_view<SimpleView>);
 static_assert(!std::ranges::__simple_view<WrongConstView>);
 static_assert(!std::ranges::__simple_view<NoConstView>);
-static_assert(!std::ranges::__simple_view<DifferentSentinel>);
+static_assert( std::ranges::__simple_view<DifferentSentinel>);
+static_assert(!std::ranges::__simple_view<WrongConstSentinel>);
Index: libcxx/include/__ranges/concepts.h
===================================================================
--- libcxx/include/__ranges/concepts.h
+++ libcxx/include/__ranges/concepts.h
@@ -80,11 +80,11 @@
     movable<_Tp> &&
     enable_view<_Tp>;
 
-  template<class _Range>
+  template <class _Range>
   concept __simple_view =
     view<_Range> && range<const _Range> &&
     same_as<iterator_t<_Range>, iterator_t<const _Range>> &&
-    same_as<sentinel_t<_Range>, iterator_t<const _Range>>;
+    same_as<sentinel_t<_Range>, sentinel_t<const _Range>>;
 
   // [range.refinements], other range refinements
   template <class _Rp, class _Tp>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116808.398222.patch
Type: text/x-patch
Size: 2686 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220107/cd022a18/attachment.bin>


More information about the libcxx-commits mailing list