-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[reland][libc] Add reverse_iterator comparisons #86188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-libc Author: Guillaume Chatelet (gchatelet) Changeshttps://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmp Full diff: https://github.com/llvm/llvm-project/pull/86188.diff 1 Files Affected:
diff --git a/libc/src/__support/CPP/iterator.h b/libc/src/__support/CPP/iterator.h
index c5bfb1912c7b74..b0fd5c9f22ae01 100644
--- a/libc/src/__support/CPP/iterator.h
+++ b/libc/src/__support/CPP/iterator.h
@@ -20,6 +20,7 @@ namespace cpp {
template <typename T> struct iterator_traits;
template <typename T> struct iterator_traits<T *> {
using reference = T &;
+ using value_type = T;
};
template <typename Iter> class reverse_iterator {
@@ -27,6 +28,8 @@ template <typename Iter> class reverse_iterator {
public:
using reference = typename iterator_traits<Iter>::reference;
+ using value_type = typename iterator_traits<Iter>::value_type;
+ using iterator_type = Iter;
LIBC_INLINE reverse_iterator() : current() {}
LIBC_INLINE constexpr explicit reverse_iterator(Iter it) : current(it) {}
@@ -38,6 +41,38 @@ template <typename Iter> class reverse_iterator {
LIBC_INLINE constexpr explicit reverse_iterator(const Other &it)
: current(it) {}
+ LIBC_INLINE friend constexpr bool operator==(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() == rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator!=(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() != rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator<(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() > rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator<=(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() >= rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator>(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() < rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator>=(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() <= rhs.base();
+ }
+
+ LIBC_INLINE constexpr iterator_type base() const { return current; }
+
LIBC_INLINE constexpr reference operator*() const {
Iter tmp = current;
return *--tmp;
|
The fix 8cfdd0e is stacked on the original commit for easier review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed this in code review of #86147. I guess without calls to these methods (in tests or sources) we don't observe diagnostics at compile time?
Indeed. |
This is a reland of llvm#86147 but with a proper `base()` function. https://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmp
This is a reland of #86147 but with a proper
base()
function.https://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmp