[all-commits] [llvm/llvm-project] a5d919: [libc++] Disallow std::prev(non_cpp17_bidi_iterato...

Hui via All-commits all-commits at lists.llvm.org
Wed Oct 23 11:42:37 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: a5d919b4b2f24c582838659fe7f30073e7285524
      https://github.com/llvm/llvm-project/commit/a5d919b4b2f24c582838659fe7f30073e7285524
  Author: Hui <hui.xie1990 at gmail.com>
  Date:   2024-10-23 (Wed, 23 Oct 2024)

  Changed paths:
    M libcxx/include/__iterator/prev.h
    A libcxx/test/libcxx/iterators/iterator.primitives/iterator.operations/prev.verify.cpp

  Log Message:
  -----------
  [libc++] Disallow std::prev(non_cpp17_bidi_iterator) (#112102)

The std::prev function appeared to work on non Cpp17BidirectionalIterators, however it
behaved strangely by being the identity function. That was extremely misleading and
potentially dangerous, since several recent iterators that are C++20 bidirectional_iterators
don't satisfy the Cpp17BidirectionalIterator named requirement. For example:

    auto zv = std::views::zip(vec1, vec2);
    auto it = zv.begin() + 5;
    auto it2 = std::prev(it); // "it2" will be the same as "it",  instead of the previous one

Here, zip_view::iterator is a c++20 random_access_iterator, but it only satisfies the
Cpp17InputIterator named requirement because its reference type is a proxy type. Hence
`std::prev` would silently accept that iterator but it would do a no-op instead of going
to the previous position.

This patch changes `std::prev(it)` to produce an error at compile-time when instantiated
with a type that is not a Cpp17BidirectionalIterator.

Fixes #109456



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list