Stop inheriting from std::iterator
std::iterator was deprecated in C++17 and after upgrading libc++, the
compiler warns about the many uses of it in ART.
p0174r1 gives a couple of reasons for its deprecation:
"The long sequence of void arguments is much less clear to the reader
than simply providing the expected typedefs in the class definition
itself, which is the approach taken by the current working draft, ...
"In addition to the reduced clarity, the iterator template also lays a
trap for the unwary, as in typical usage it will be a dependent base
class, which means it will not be looking into during name lookup from
within the class or its member functions."
The first reason is illustrated by the comments in
BitTable::const_iterator. The second reason is illustrated by the
various "using foo = typename std::iterator<...>::foo" declarations.
Follow p0174r1's advice and simply provide the 5 typedefs in each
iterator class.
Bug: b/175635923
Test: treehugger
Change-Id: I2fba5af68eb05fd0a8ba5e2add0c8b8ed1ebee1a
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index 1ad11d8..a44e349 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -2194,9 +2194,14 @@
std::ostream& operator<<(std::ostream& os, const HInstruction& rhs);
// Iterates over the Environments
-class HEnvironmentIterator : public ValueObject,
- public std::iterator<std::forward_iterator_tag, HEnvironment*> {
+class HEnvironmentIterator : public ValueObject {
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = HEnvironment*;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
+
explicit HEnvironmentIterator(HEnvironment* cur) : cur_(cur) {}
HEnvironment* operator*() const {
@@ -2915,9 +2920,14 @@
};
template <typename InnerIter>
-struct HSTLInstructionIterator : public ValueObject,
- public std::iterator<std::forward_iterator_tag, HInstruction*> {
+struct HSTLInstructionIterator : public ValueObject {
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = HInstruction*;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
+
static_assert(std::is_same_v<InnerIter, HBackwardInstructionIterator> ||
std::is_same_v<InnerIter, HInstructionIterator> ||
std::is_same_v<InnerIter, HInstructionIteratorHandleChanges>,
diff --git a/dexlayout/dex_ir.h b/dexlayout/dex_ir.h
index a4b43b6..229e948 100644
--- a/dexlayout/dex_ir.h
+++ b/dexlayout/dex_ir.h
@@ -110,13 +110,14 @@
DISALLOW_COPY_AND_ASSIGN(AbstractDispatcher);
};
-template<class T> class Iterator : public std::iterator<std::random_access_iterator_tag, T> {
+template <class T>
+class Iterator {
public:
- using value_type = typename std::iterator<std::random_access_iterator_tag, T>::value_type;
- using difference_type =
- typename std::iterator<std::random_access_iterator_tag, value_type>::difference_type;
- using pointer = typename std::iterator<std::random_access_iterator_tag, value_type>::pointer;
- using reference = typename std::iterator<std::random_access_iterator_tag, value_type>::reference;
+ using iterator_category = std::random_access_iterator_tag;
+ using value_type = T;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
Iterator(const Iterator&) = default;
Iterator(Iterator&&) noexcept = default;
diff --git a/libartbase/base/bit_table.h b/libartbase/base/bit_table.h
index 227f5eb..eb97b54 100644
--- a/libartbase/base/bit_table.h
+++ b/libartbase/base/bit_table.h
@@ -153,13 +153,13 @@
template<typename Accessor>
class BitTable : public BitTableBase<Accessor::kNumColumns> {
public:
- class const_iterator : public std::iterator<std::random_access_iterator_tag,
- /* value_type */ Accessor,
- /* difference_type */ int32_t,
- /* pointer */ void,
- /* reference */ void> {
+ class const_iterator {
public:
+ using iterator_category = std::random_access_iterator_tag;
+ using value_type = Accessor;
using difference_type = int32_t;
+ using pointer = void;
+ using reference = void;
const_iterator() {}
const_iterator(const BitTable* table, uint32_t row) : table_(table), row_(row) {}
const_iterator operator+(difference_type n) { return const_iterator(table_, row_ + n); }
@@ -189,6 +189,7 @@
DCHECK_LT(row_ + index, table_->NumRows());
return Accessor(table_, row_ + index);
}
+
private:
const BitTable* table_ = nullptr;
uint32_t row_ = 0;
diff --git a/libartbase/base/bit_utils_iterator.h b/libartbase/base/bit_utils_iterator.h
index bfcff86..296280e 100644
--- a/libartbase/base/bit_utils_iterator.h
+++ b/libartbase/base/bit_utils_iterator.h
@@ -32,14 +32,19 @@
// Using the Curiously Recurring Template Pattern to implement everything shared
// by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*().
template <typename T, typename Iter>
-class BitIteratorBase
- : public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, void> {
+class BitIteratorBase {
static_assert(std::is_integral_v<T>, "T must be integral");
static_assert(std::is_unsigned_v<T>, "T must be unsigned");
static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size");
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = uint32_t;
+ using difference_type = ptrdiff_t;
+ using pointer = void;
+ using reference = void;
+
BitIteratorBase() : bits_(0u) { }
explicit BitIteratorBase(T bits) : bits_(bits) { }
diff --git a/libartbase/base/bit_vector.h b/libartbase/base/bit_vector.h
index 071577b..a710c5d 100644
--- a/libartbase/base/bit_vector.h
+++ b/libartbase/base/bit_vector.h
@@ -51,9 +51,14 @@
* // Use idx.
* }
*/
- class IndexIterator :
- public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, uint32_t> {
+ class IndexIterator {
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = uint32_t;
+ using difference_type = ptrdiff_t;
+ using pointer = void;
+ using reference = uint32_t;
+
bool operator==(const IndexIterator& other) const;
bool operator!=(const IndexIterator& other) const {
diff --git a/libartbase/base/intrusive_forward_list.h b/libartbase/base/intrusive_forward_list.h
index 06dd407..f7f7395 100644
--- a/libartbase/base/intrusive_forward_list.h
+++ b/libartbase/base/intrusive_forward_list.h
@@ -59,8 +59,14 @@
class IntrusiveForwardList;
template <typename T, typename HookTraits>
-class IntrusiveForwardListIterator : public std::iterator<std::forward_iterator_tag, T> {
+class IntrusiveForwardListIterator {
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = T;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
+
// Construct/copy/destroy (except the private constructor used by IntrusiveForwardList<>).
IntrusiveForwardListIterator() : hook_(nullptr) { }
IntrusiveForwardListIterator(const IntrusiveForwardListIterator& src) = default;
diff --git a/libartbase/base/stl_util.h b/libartbase/base/stl_util.h
index 2c9547f..cb3e4cb 100644
--- a/libartbase/base/stl_util.h
+++ b/libartbase/base/stl_util.h
@@ -156,10 +156,14 @@
}
template <typename IterLeft, typename IterRight>
-class ZipLeftIter : public std::iterator<
- std::forward_iterator_tag,
- std::pair<typename IterLeft::value_type, typename IterRight::value_type>> {
+class ZipLeftIter {
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = std::pair<typename IterLeft::value_type, typename IterRight::value_type>;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
+
ZipLeftIter(IterLeft left, IterRight right) : left_iter_(left), right_iter_(right) {}
ZipLeftIter<IterLeft, IterRight>& operator++() {
++left_iter_;
@@ -186,8 +190,14 @@
IterRight right_iter_;
};
-class CountIter : public std::iterator<std::forward_iterator_tag, size_t, size_t, size_t, size_t> {
+class CountIter {
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = size_t;
+ using difference_type = size_t;
+ using pointer = size_t;
+ using reference = size_t;
+
CountIter() : count_(0) {}
explicit CountIter(size_t count) : count_(count) {}
CountIter& operator++() {
@@ -238,9 +248,14 @@
}
template <typename RealIter, typename Filter>
-struct FilterIterator
- : public std::iterator<std::forward_iterator_tag, typename RealIter::value_type> {
+struct FilterIterator {
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = typename RealIter::value_type;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
+
FilterIterator(RealIter rl,
Filter cond,
std::optional<RealIter> end = std::nullopt)
@@ -323,8 +338,14 @@
}
// Helper struct for iterating a split-string without allocation.
-struct SplitStringIter : public std::iterator<std::forward_iterator_tag, std::string_view> {
+struct SplitStringIter {
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = std::string_view;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
+
// Direct iterator constructor. The iteration state is only the current index.
// We use that with the split char and the full string to get the current and
// next segment.
diff --git a/libartbase/base/stride_iterator.h b/libartbase/base/stride_iterator.h
index 6a7e4be..7d1b9fc 100644
--- a/libartbase/base/stride_iterator.h
+++ b/libartbase/base/stride_iterator.h
@@ -23,11 +23,14 @@
namespace art {
-template<typename T>
-class StrideIterator : public std::iterator<std::random_access_iterator_tag, T> {
+template <typename T>
+class StrideIterator {
public:
- using difference_type =
- typename std::iterator<std::random_access_iterator_tag, T>::difference_type;
+ using iterator_category = std::random_access_iterator_tag;
+ using value_type = T;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
StrideIterator(const StrideIterator&) = default;
StrideIterator(StrideIterator&&) noexcept = default;
diff --git a/libdexfile/dex/class_accessor.h b/libdexfile/dex/class_accessor.h
index 1061ff6..401195c 100644
--- a/libdexfile/dex/class_accessor.h
+++ b/libdexfile/dex/class_accessor.h
@@ -178,11 +178,13 @@
};
template <typename DataType>
- class DataIterator : public std::iterator<std::forward_iterator_tag, DataType> {
+ class DataIterator {
public:
- using value_type = typename std::iterator<std::forward_iterator_tag, DataType>::value_type;
- using difference_type =
- typename std::iterator<std::forward_iterator_tag, value_type>::difference_type;
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = DataType;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
DataIterator(const DexFile& dex_file,
uint32_t position,
diff --git a/libdexfile/dex/class_iterator.h b/libdexfile/dex/class_iterator.h
index 8ed585b..56913a9 100644
--- a/libdexfile/dex/class_iterator.h
+++ b/libdexfile/dex/class_iterator.h
@@ -41,10 +41,13 @@
};
// Iterator for visiting classes in a Dex file.
-class ClassIterator : public std::iterator<std::forward_iterator_tag, ClassIteratorData> {
+class ClassIterator {
public:
- using value_type = std::iterator<std::forward_iterator_tag, ClassIteratorData>::value_type;
- using difference_type = std::iterator<std::forward_iterator_tag, value_type>::difference_type;
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = ClassIteratorData;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
ClassIterator(const DexFile& dex_file, uint32_t class_def_idx)
: data_(dex_file, class_def_idx) {}
diff --git a/libdexfile/dex/dex_instruction_iterator.h b/libdexfile/dex/dex_instruction_iterator.h
index 6c7f42a..6f661d9 100644
--- a/libdexfile/dex/dex_instruction_iterator.h
+++ b/libdexfile/dex/dex_instruction_iterator.h
@@ -57,11 +57,13 @@
};
// Base helper class to prevent duplicated comparators.
-class DexInstructionIteratorBase : public
- std::iterator<std::forward_iterator_tag, DexInstructionPcPair> {
+class DexInstructionIteratorBase {
public:
- using value_type = std::iterator<std::forward_iterator_tag, DexInstructionPcPair>::value_type;
- using difference_type = std::iterator<std::forward_iterator_tag, value_type>::difference_type;
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = DexInstructionPcPair;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
explicit DexInstructionIteratorBase(const Instruction* inst, uint32_t dex_pc)
: data_(reinterpret_cast<const uint16_t*>(inst), dex_pc) {}
diff --git a/runtime/mirror/object_array.h b/runtime/mirror/object_array.h
index 9a53708..c6b1e01 100644
--- a/runtime/mirror/object_array.h
+++ b/runtime/mirror/object_array.h
@@ -162,11 +162,17 @@
// Everything is NO_THREAD_SAFETY_ANALYSIS to work-around STL incompat with thread-annotations.
// Everything should have REQUIRES_SHARED(Locks::mutator_lock_).
template <typename T, typename Container>
-class ArrayIter : public std::iterator<std::forward_iterator_tag, ObjPtr<T>> {
+class ArrayIter {
private:
using Iter = ArrayIter<T, Container>;
public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = ObjPtr<T>;
+ using difference_type = ptrdiff_t;
+ using pointer = value_type*;
+ using reference = value_type&;
+
ArrayIter(Container array, int32_t idx) NO_THREAD_SAFETY_ANALYSIS : array_(array), idx_(idx) {
CheckIdx();
}