[libcxx-commits] [libcxx] [libc++][NFC] Make data members of counted_iterator private (PR #70491)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Sun Nov 5 16:44:29 PST 2023


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/70491

>From 74d6ce11e431da5e72d8cbbb8a80db65bd1f8b34 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Fri, 27 Oct 2023 14:26:21 -0400
Subject: [PATCH 1/2] [libc++][NFC] Make data members of counted_iterator
 private

There's no reason for them to be public AFAICT. I came across this while
auditing the code bases for places where we'd be using [[no_unique_address]]
incorrectly. Since one of these members is marked as [[no_unique_address]],
this is an extra reason to keep this private to ensure the member isn't
escaped anywhere.
---
 libcxx/include/__iterator/counted_iterator.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__iterator/counted_iterator.h b/libcxx/include/__iterator/counted_iterator.h
index 41b7e57d28c1451..5e82a496c8093bd 100644
--- a/libcxx/include/__iterator/counted_iterator.h
+++ b/libcxx/include/__iterator/counted_iterator.h
@@ -73,10 +73,10 @@ class counted_iterator
   , public __counted_iterator_category<_Iter>
   , public __counted_iterator_value_type<_Iter>
 {
-public:
   _LIBCPP_NO_UNIQUE_ADDRESS _Iter __current_ = _Iter();
   iter_difference_t<_Iter> __count_ = 0;
 
+public:
   using iterator_type = _Iter;
   using difference_type = iter_difference_t<_Iter>;
 

>From 7c30a0308dd4a85a2bc170f708aa5aecf060d53d Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Sun, 5 Nov 2023 17:42:53 -0700
Subject: [PATCH 2/2] Make counted_iterator friend with other instantiations of
 itself

---
 libcxx/include/__iterator/counted_iterator.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/__iterator/counted_iterator.h b/libcxx/include/__iterator/counted_iterator.h
index 5e82a496c8093bd..306b5f36c6e060f 100644
--- a/libcxx/include/__iterator/counted_iterator.h
+++ b/libcxx/include/__iterator/counted_iterator.h
@@ -73,9 +73,6 @@ class counted_iterator
   , public __counted_iterator_category<_Iter>
   , public __counted_iterator_value_type<_Iter>
 {
-  _LIBCPP_NO_UNIQUE_ADDRESS _Iter __current_ = _Iter();
-  iter_difference_t<_Iter> __count_ = 0;
-
 public:
   using iterator_type = _Iter;
   using difference_type = iter_difference_t<_Iter>;
@@ -297,6 +294,12 @@ class counted_iterator
                                  "Iterators must not be past end of range.");
     return ranges::iter_swap(__x.__current_, __y.__current_);
   }
+
+private:
+  _LIBCPP_NO_UNIQUE_ADDRESS _Iter __current_ = _Iter();
+  iter_difference_t<_Iter> __count_ = 0;
+  template<input_or_output_iterator _OtherIter>
+  friend class counted_iterator;
 };
 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(counted_iterator);
 



More information about the libcxx-commits mailing list