Skip to content

Commit

Permalink
C++17 friendly iterator implementation
Browse files Browse the repository at this point in the history
Get rid of std::iterator inheritence for `c10::DictIterator`, `c10::IListRefIterator` and `c10::ListIterator`

Followup after #90174

Fixes deprecation warning and extension compilation failures using VC++
that raises following errors:
```
C:\actions-runner\_work\pytorch\pytorch\build\win_tmp\build\torch\include\ATen/core/IListRef.h(517): error C4996: 'std::iterator<std::bidirectional_iterator_tag,T,ptrdiff_t,T *,T &>::value_type': warning STL4015: The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17. (The <iterator> header is NOT deprecated.) The C++ Standard has never required user-defined iterators to derive from std::iterator. To fix this warning, stop deriving from std::iterator and start providing publicly accessible typedefs named iterator_category, value_type, difference_type, pointer, and reference. Note that value_type is required to be non-const, even for constant iterators. You can define _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.

C:\actions-runner\_work\pytorch\pytorch\build\win_tmp\build\torch\include\ATen/core/List.h(169): error C4996: 'std::iterator<std::random_access_iterator_tag,T,ptrdiff_t,T *,T &>::difference_type': warning STL4015: The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17. (The <iterator> header is NOT deprecated.) The C++ Standard has never required user-defined iterators to derive from std::iterator. To fix this warning, stop deriving from std::iterator and start providing publicly accessible typedefs named iterator_category, value_type, difference_type, pointer, and reference. Note that value_type is required to be non-const, even for constant iterators. You can define _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.

```
  • Loading branch information
malfet committed Dec 7, 2022
1 parent b0bd5c4 commit edf7b27
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
11 changes: 9 additions & 2 deletions aten/src/ATen/core/Dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ class DictEntryRef final {
// this wraps map_type::iterator to make sure user code can't rely
// on it being the type of the underlying map.
template<class Key, class Value, class Iterator>
class DictIterator final : public std::iterator<std::forward_iterator_tag, DictEntryRef<Key, Value, Iterator>> {
class DictIterator final {
public:
// C++17 friendly std::iterator implementation
using iterator_category = std::forward_iterator_tag;
using value_type = DictEntryRef<Key, Value, Iterator>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;

explicit DictIterator() = default;
~DictIterator() = default;

Expand Down Expand Up @@ -136,7 +143,7 @@ class DictIterator final : public std::iterator<std::forward_iterator_tag, DictE
return &entryRef_;
}

friend typename std::iterator<std::random_access_iterator_tag, DictEntryRef<Key, Value, Iterator>>::difference_type operator-(const DictIterator& lhs, const DictIterator& rhs) {
friend difference_type operator-(const DictIterator& lhs, const DictIterator& rhs) {
return lhs.entryRef_.iterator_ - rhs.entryRef_.iterator_;
}

Expand Down
9 changes: 8 additions & 1 deletion aten/src/ATen/core/IListRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ using MaterializedIListRef = std::vector<MaterializedIListRefElem<T>>;
* than 0.
*/
template <typename T>
class IListRefIterator : public std::iterator<std::bidirectional_iterator_tag, T> {
class IListRefIterator {
private:
#define DEFINE_FRIEND_CLASS(TAG, ...) \
friend class detail::IListRefTagImpl<IListRefTag::TAG, T>; \
Expand All @@ -371,6 +371,13 @@ class IListRefIterator : public std::iterator<std::bidirectional_iterator_tag, T
#undef DEFINE_FRIEND_CLASS

public:
// C++17 friendly std::iterator implementation
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;

using unboxed_iterator_type = typename detail::
IListRefTagImpl<IListRefTag::Unboxed, T>::list_type::const_iterator;
using boxed_iterator_type = typename detail::
Expand Down
16 changes: 9 additions & 7 deletions aten/src/ATen/core/List.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ class ListElementReference final {
// this wraps vector::iterator to make sure user code can't rely
// on it being the type of the underlying vector.
template <class T, class Iterator>
class ListIterator final : public std::iterator<
std::random_access_iterator_tag,
T,
std::ptrdiff_t,
T*,
ListElementReference<T, Iterator>> {
class ListIterator final {
public:
// C++17 friendly std::iterator implementation
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = ListElementReference<T, Iterator>;

explicit ListIterator() = default;
~ListIterator() = default;

Expand Down Expand Up @@ -166,7 +168,7 @@ class ListIterator final : public std::iterator<
return ListIterator{iterator_ - offset};
}

friend typename std::iterator<std::random_access_iterator_tag, T>::difference_type operator-(const ListIterator& lhs, const ListIterator& rhs) {
friend difference_type operator-(const ListIterator& lhs, const ListIterator& rhs) {
return lhs.iterator_ - rhs.iterator_;
}

Expand Down

0 comments on commit edf7b27

Please sign in to comment.