From 507f1d82f6c8d13a04d2dc554124b02e4aea6cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20P=C3=A9ron?= Date: Wed, 18 Jun 2025 17:22:11 +0200 Subject: [PATCH] graph: fix iterator compatibility since C++17 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add missing iterator typedefs to custom iterator classes when std::iterator inheritance is deprecated since C++17. Signed-off-by: Clément Péron --- ortools/base/proto_enum_utils.h | 13 ++++++++++++- ortools/graph/graph.h | 8 +++++++- ortools/graph/iterators.h | 14 ++++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ortools/base/proto_enum_utils.h b/ortools/base/proto_enum_utils.h index a78dd61a72..bdf0331056 100644 --- a/ortools/base/proto_enum_utils.h +++ b/ortools/base/proto_enum_utils.h @@ -175,8 +175,19 @@ namespace internal { template class RepeatedEnumView { public: - class Iterator : public std::iterator { + class Iterator +#if __cplusplus < 201703L + : public std::iterator +#endif + { public: + using difference_type = ptrdiff_t; + using value_type = E; +#if __cplusplus >= 201703L + using iterator_category = std::input_iterator_tag; + using pointer = E*; + using reference = E&; +#endif explicit Iterator(RepeatedField::const_iterator ptr) : ptr_(ptr) {} bool operator==(const Iterator& it) const { return ptr_ == it.ptr_; } bool operator!=(const Iterator& it) const { return ptr_ != it.ptr_; } diff --git a/ortools/graph/graph.h b/ortools/graph/graph.h index c8b7ef0b83..db3f0e2bcb 100644 --- a/ortools/graph/graph.h +++ b/ortools/graph/graph.h @@ -315,7 +315,7 @@ class BaseGraph { template class ArcPropertyIterator -#if __cplusplus < 202002L +#if __cplusplus < 201703L : public std::iterator #endif { @@ -324,6 +324,11 @@ class ArcPropertyIterator // TODO(b/385094969): This should be `NodeIndex` for integers, // `NodeIndex::value_type` for strong signed integer types. using difference_type = std::ptrdiff_t; +#if __cplusplus >= 201703L && __cplusplus < 202002L + using iterator_category = std::input_iterator_tag; + using pointer = PropertyT*; + using reference = PropertyT&; +#endif ArcPropertyIterator() = default; @@ -346,6 +351,7 @@ class ArcPropertyIterator const ArcPropertyIterator& r) { return l.arc_it_ == r.arc_it_; } + friend bool operator!=(const ArcPropertyIterator& l, const ArcPropertyIterator& r) { return !(l == r); diff --git a/ortools/graph/iterators.h b/ortools/graph/iterators.h index 73f67a07bd..50fd5335b0 100644 --- a/ortools/graph/iterators.h +++ b/ortools/graph/iterators.h @@ -124,13 +124,18 @@ class IntegerRangeIterator // TODO(b/385094969): In C++17, `std::iterator_traits` required // explicitly specifying the iterator category. Remove this when backwards // compatibility with C++17 is no longer needed. -#if __cplusplus < 202002L +#if __cplusplus < 201703L : public std::iterator #endif { public: using difference_type = ptrdiff_t; using value_type = IntegerType; +#if __cplusplus >= 201703L && __cplusplus < 202002L + using iterator_category = std::input_iterator_tag; + using pointer = IntegerType*; + using reference = IntegerType&; +#endif IntegerRangeIterator() : index_{} {} @@ -243,13 +248,18 @@ class IntegerRange : public BeginEndWrapper> { // different iterators with the same index type and sentinel. template class ChasingIterator -#if __cplusplus < 202002L +#if __cplusplus < 201703L : public std::iterator #endif { public: using difference_type = ptrdiff_t; using value_type = IndexT; +#if __cplusplus >= 201703L && __cplusplus < 202002L + using iterator_category = std::input_iterator_tag; + using pointer = IndexT*; + using reference = IndexT&; +#endif ChasingIterator() : index_(sentinel), next_(nullptr) {}