[libcxx-commits] [PATCH] D108575: [libcxx] Define insert_iterator::iter with ranges::iterator_t

Joe Loser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 25 08:14:27 PDT 2021


jloser updated this revision to Diff 368646.
jloser added a comment.

Simplify test per Arthur's suggestions


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108575

Files:
  libcxx/include/__iterator/insert_iterator.h
  libcxx/test/std/iterators/predef.iterators/insert.iterators/insert.iterator/types.pass.cpp


Index: libcxx/test/std/iterators/predef.iterators/insert.iterators/insert.iterator/types.pass.cpp
===================================================================
--- libcxx/test/std/iterators/predef.iterators/insert.iterators/insert.iterator/types.pass.cpp
+++ libcxx/test/std/iterators/predef.iterators/insert.iterators/insert.iterator/types.pass.cpp
@@ -28,6 +28,7 @@
 //   typedef void                   pointer;
 // };
 
+#include <cassert>
 #include <iterator>
 #include <type_traits>
 #include <vector>
@@ -70,9 +71,38 @@
 #endif
 }
 
+struct NoIteratorAlias {
+  long data_[3] = {};
+  using value_type = int;
+  long* begin() { return nullptr; }
+  constexpr long* insert(long* pos, int value) {
+    *pos = value;
+    return pos;
+  }
+};
+
+#if TEST_STD_VER > 17
+constexpr bool test_no_iterator_alias() {
+  NoIteratorAlias c;
+  auto it = std::insert_iterator<NoIteratorAlias>(c, c.data_);
+  *it++ = 1;
+  *it++ = 2;
+  assert(c.data_[0] == 1);
+  assert(c.data_[1] == 2);
+  assert(c.data_[2] == 0);
+  return true;
+}
+#endif // TEST_STD_VER > 17
+
 int main(int, char**)
 {
     test<std::vector<int> >();
 
-  return 0;
+#if TEST_STD_VER > 17
+    test<NoIteratorAlias>();
+    test_no_iterator_alias();
+    static_assert(test_no_iterator_alias());
+#endif // TEST_STD_VER > 17
+
+    return 0;
 }
Index: libcxx/include/__iterator/insert_iterator.h
===================================================================
--- libcxx/include/__iterator/insert_iterator.h
+++ libcxx/include/__iterator/insert_iterator.h
@@ -14,6 +14,7 @@
 #include <__iterator/iterator.h>
 #include <__iterator/iterator_traits.h>
 #include <__memory/addressof.h>
+#include <__ranges/access.h>
 #include <__utility/move.h>
 #include <cstddef>
 
@@ -36,8 +37,12 @@
 _LIBCPP_SUPPRESS_DEPRECATED_POP
 protected:
     _Container* container;
-    typename _Container::iterator iter; // FIXME: `ranges::iterator_t<Container>` in C++20 mode
-public:
+#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
+    ranges::iterator_t<_Container> iter;
+#else
+    typename _Container::iterator iter;
+#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
+  public:
     typedef output_iterator_tag iterator_category;
     typedef void value_type;
 #if _LIBCPP_STD_VER > 17
@@ -49,7 +54,7 @@
     typedef void reference;
     typedef _Container container_type;
 
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, decltype(iter) __i)
         : container(_VSTD::addressof(__x)), iter(__i) {}
     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
         {iter = container->insert(iter, __value_); ++iter; return *this;}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108575.368646.patch
Type: text/x-patch
Size: 2880 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210825/5f480048/attachment.bin>


More information about the libcxx-commits mailing list