[libcxx-commits] [libcxx] [libc++][test][NFC] Fix `overload_compare_iterator::iterator_category` (PR #112165)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 14 01:02:34 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

<details>
<summary>Changes</summary>

`overload_compare_iterator` only supports operations required for forward iterators. On the other hand, it is used for output iterators of uninitialized memory algorithms, which requires it to be forward iterator.

As a result, `overload_compare_iterator<I>::iterator_category` should always be `std::forward_iterator_tag` if we don't extends its ability. The correct `iterator_category` can prevent standard library implementations like MSVC STL to attempt random access operations on it.

Fixes #<!-- -->74756.

---
Full diff: https://github.com/llvm/llvm-project/pull/112165.diff


1 Files Affected:

- (modified) libcxx/test/std/utilities/memory/specialized.algorithms/overload_compare_iterator.h (+6-1) 


``````````diff
diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/overload_compare_iterator.h b/libcxx/test/std/utilities/memory/specialized.algorithms/overload_compare_iterator.h
index d5dcb08c37ed6f..f3b37292e717a1 100644
--- a/libcxx/test/std/utilities/memory/specialized.algorithms/overload_compare_iterator.h
+++ b/libcxx/test/std/utilities/memory/specialized.algorithms/overload_compare_iterator.h
@@ -12,6 +12,7 @@
 
 #include <iterator>
 #include <memory>
+#include <type_traits>
 
 #include "test_macros.h"
 
@@ -21,11 +22,15 @@
 // See https://github.com/llvm/llvm-project/issues/69334 for details.
 template <class Iterator>
 struct overload_compare_iterator {
+  static_assert(
+      std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<Iterator>::iterator_category>::value,
+      "overload_compare_iterator can only adapt forward iterators");
+
   using value_type        = typename std::iterator_traits<Iterator>::value_type;
   using difference_type   = typename std::iterator_traits<Iterator>::difference_type;
   using reference         = typename std::iterator_traits<Iterator>::reference;
   using pointer           = typename std::iterator_traits<Iterator>::pointer;
-  using iterator_category = typename std::iterator_traits<Iterator>::iterator_category;
+  using iterator_category = std::forward_iterator_tag;
 
   overload_compare_iterator() = default;
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/112165


More information about the libcxx-commits mailing list