[libc-commits] [libc] [libc][cpp] reverse_iterator support (PR #85702)

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Wed Mar 20 08:17:15 PDT 2024


================
@@ -45,9 +48,29 @@ template <class T, size_t N> struct array {
 
   LIBC_INLINE constexpr iterator begin() { return Data; }
   LIBC_INLINE constexpr const_iterator begin() const { return Data; }
+  LIBC_INLINE constexpr const_iterator cbegin() const { return begin(); }
 
   LIBC_INLINE constexpr iterator end() { return Data + N; }
   LIBC_INLINE constexpr const_iterator end() const { return Data + N; }
+  LIBC_INLINE constexpr const_iterator cend() const { return end(); }
+
+  LIBC_INLINE constexpr reverse_iterator rbegin() {
+    return reverse_iterator{end()};
+  }
+  LIBC_INLINE constexpr const_reverse_iterator rbegin() const {
+    return const_reverse_iterator{end()};
+  }
+  LIBC_INLINE constexpr const_reverse_iterator crbegin() const {
+    return rbegin();
+  }
+
+  LIBC_INLINE constexpr reverse_iterator rend() {
+    return reverse_iterator{begin()};
+  }
+  LIBC_INLINE constexpr const_reverse_iterator rend() const {
+    return const_reverse_iterator{begin()};
+  }
+  LIBC_INLINE constexpr const_reverse_iterator crend() const { return rend(); }
----------------
gchatelet wrote:

Hehe! I didn't mean to confuse you with `[[nodiscard]]` but I'm amazed by the outcome!

Tangentially I think we're missing the following functions for `reverse_iterator` to be useful.
```
  LIBC_INLINE friend constexpr bool operator==(const reverse_iterator &lhs,
                                               const reverse_iterator &rhs) {
    return lhs.current == rhs.current;
  }
  LIBC_INLINE friend constexpr bool operator!=(const reverse_iterator &lhs,
                                               const reverse_iterator &rhs) {
    return lhs.current != rhs.current;
  }
```

as in `for (auto itr = value.rbegin(); itr **!=** value.rend(); ++itr)`.

https://github.com/llvm/llvm-project/pull/85702


More information about the libc-commits mailing list