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

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


https://github.com/frederick-vs-ja created https://github.com/llvm/llvm-project/pull/112165

`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.

>From 73107556690daaec6ecea02768ac5a0985dbca45 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Mon, 14 Oct 2024 16:00:23 +0800
Subject: [PATCH] [libc++][test][NFC] Fix
 `overload_compare_iterator::iterator_category`

`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.
---
 .../specialized.algorithms/overload_compare_iterator.h     | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

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;
 



More information about the libcxx-commits mailing list