[libcxx-commits] [PATCH] D113868: [libcxx] Cast to the right `difference_type` in `std::search` and `std::find_end`

Arthur O'Dwyer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Nov 14 17:22:13 PST 2021


Quuxplusone requested changes to this revision.
Quuxplusone added a comment.
This revision now requires changes to proceed.

https://en.cppreference.com/w/cpp/algorithm/ranges/find_end
http://eel.is/c++draft/alg.find.end

Notice that `std::ranges::find_end` is specifically specified to return an expression involving `i + (last2 - first2)` where `i` is an `I1` — implying that `I1 + D2` ought to be valid, even though `ranges::find_end` doesn't list that as a Constraint. So I'm inclined to say that the culprit here is the user's iterator type that is implicitly convertible to raw pointer; fix that, and you'll have a lot less grief as you move toward C++20.

If we pursue this PR, I think we should at least use an implicit conversion, not an explicit conversion; I trust `_D1 d1 = __len2 - 1` to do-the-right-thing-or-error-out waaay more than I trust `static_cast<_D1>(__len2 - 1)`.

Finally, this needs tests. I recommend copying the approach of `libcxx/test/std/algorithms/robust_against_adl.pass.cpp`: make two iterator types that play poorly together, as in the OP, and then test them against //all// the standard algorithms that take multiple iterator types. (This can be a compile-only test.)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D113868/new/

https://reviews.llvm.org/D113868



More information about the libcxx-commits mailing list