[PATCH] D113158: ADT: Fix const-correctness of iterator adaptors

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 3 17:42:00 PDT 2021


dexonsmith created this revision.
dexonsmith added reviewers: mehdi_amini, dblaikie.
dexonsmith requested review of this revision.
Herald added a project: LLVM.

This fixes const-correctness of iterator adaptors, dropping non-`const`
overloads for `operator*()`.

Iterators, like the pointers that they generalize, have two types of
`const`.

The `const` qualifier on members indicates whether the iterator itself
can be changed. This is analagous to `int *const`.

The `const` qualifier on return values of `operator*()`, `operator[]()`,
and `operator->()` controls whether the the pointed-to value can be
changed. This is analogous to `const int *`.

Since `operator*()` does not (in principle) change the iterator, then
there should only be one definition, which is `const`-qualified. E.g.,
iterators wrapping `int*` should look like:

  int *operator*() const; // always const-qualified, no overloads

ba7a6b314fd14bb2c9ff5d3f4fe2b6525514cada <https://reviews.llvm.org/rGba7a6b314fd14bb2c9ff5d3f4fe2b6525514cada> changed `iterator_adaptor_base`
away from this to work around bugs in other iterator adaptors. This
commit reverts the change to `iterator_adaptor_base`, adds a test, and
fixes the root cause of the bugs in other adaptors.

This also updates the documented requirements for
`iterator_facade_base`:

  /// OLD:
  ///   - const T &operator*() const;
  ///   - T &operator*();
  
  /// New:
  ///   - T &operator*() const;

In a future commit we might also clean up `iterator_facade`'s overloads
of `operator->()` and `operator[]()`. These already (correctly) return
non-`const` proxies regardless of the iterator's `const` qualifier.


https://reviews.llvm.org/D113158

Files:
  llvm/include/llvm/ADT/STLExtras.h
  llvm/include/llvm/ADT/iterator.h
  llvm/unittests/ADT/IteratorTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113158.384622.patch
Type: text/x-patch
Size: 6476 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211104/fa9f98b6/attachment.bin>


More information about the llvm-commits mailing list