Tweak Dict iterators to comply with std::ranges concept expectations.
* operator*() needs to model shallow constness.
* There needs to be a common reference type between the value and
reference types. In this case, the existing reference type was
effectively also the value type, because there doesn't exist any
separate instance of that "value" anywhere; the returned reference is
bespoke-constructed.
Bug: 386918226
Change-Id: I576f3d65e4d98dd052096caad5d54fc1aaef38ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6164118
Commit-Queue: Daniel Cheng <[email protected]>
Code-Coverage: [email protected] <[email protected]>
Commit-Queue: Peter Kasting <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Auto-Submit: Peter Kasting <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1404973}
diff --git a/base/value_iterators.h b/base/value_iterators.h
index ab5c2cc..49d78085 100644
--- a/base/value_iterators.h
+++ b/base/value_iterators.h
@@ -24,14 +24,13 @@
// This iterator closely resembles DictStorage::iterator, with one
// important exception. It abstracts the underlying unique_ptr away, meaning its
-// value_type is std::pair<const std::string, Value>. It's reference type is a
-// std::pair<const std::string&, Value&>, so that callers have read-write
-// access without incurring a copy.
+// reference type is a std::pair<const std::string&, Value&>, so that callers
+// have read-write access without incurring a copy.
class BASE_EXPORT dict_iterator {
public:
using difference_type = DictStorage::iterator::difference_type;
- using value_type = std::pair<const std::string, Value>;
- using reference = std::pair<const std::string&, Value&>;
+ using value_type = std::pair<const std::string&, Value&>;
+ using reference = value_type;
using iterator_category = std::bidirectional_iterator_tag;
class pointer {
@@ -52,8 +51,8 @@
dict_iterator& operator=(const dict_iterator& dict_iter);
~dict_iterator();
- reference operator*();
- pointer operator->();
+ reference operator*() const;
+ pointer operator->() const;
dict_iterator& operator++();
dict_iterator operator++(int);
@@ -80,14 +79,13 @@
// This iterator closely resembles DictStorage::const_iterator, with one
// important exception. It abstracts the underlying unique_ptr away, meaning its
-// value_type is std::pair<const std::string, Value>. It's reference type is a
-// std::pair<const std::string&, const Value&>, so that callers have read-only
-// access without incurring a copy.
+// reference type is a std::pair<const std::string&, const Value&>, so that
+// callers have read-only access without incurring a copy.
class BASE_EXPORT const_dict_iterator {
public:
using difference_type = DictStorage::const_iterator::difference_type;
- using value_type = std::pair<const std::string, Value>;
- using reference = std::pair<const std::string&, const Value&>;
+ using value_type = std::pair<const std::string&, const Value&>;
+ using reference = value_type;
using iterator_category = std::bidirectional_iterator_tag;
class pointer {