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

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Mar 19 08:21:29 PDT 2024


================
@@ -0,0 +1,62 @@
+//===-- Standalone implementation of iterator -------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ITERATOR_H
+#define LLVM_LIBC_SRC___SUPPORT_CPP_ITERATOR_H
+
+#include "src/__support/CPP/type_traits/enable_if.h"
+#include "src/__support/CPP/type_traits/is_convertible.h"
+#include "src/__support/CPP/type_traits/is_same.h"
+#include "src/__support/macros/attributes.h"
+
+namespace LIBC_NAMESPACE {
+namespace cpp {
+
+template <typename T> struct iterator_traits;
+template <typename T> struct iterator_traits<T *> {
+  using reference = T &;
+};
+
+template <typename Iter> class reverse_iterator {
+  Iter current;
+
+public:
+  using reference = typename iterator_traits<Iter>::reference;
+
+  reverse_iterator() : current() {}
+  constexpr explicit reverse_iterator(Iter it) : current(it) {}
+
+  template <typename Other,
+            cpp::enable_if_t<!cpp::is_same_v<Iter, Other> &&
+                                 cpp::is_convertible_v<const Other &, Iter>,
+                             int> = 0>
+  constexpr explicit reverse_iterator(const Other &it) : current(it) {}
+
+  constexpr reference operator*() const {
----------------
nickdesaulniers wrote:

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4640.pdf S24.5.1.1 "Class template reverse_iterator" has an example. It's not using [[nodiscard]]. Same for https://isocpp.org/files/papers/N4860.pdf (S23.5.1.1).

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


More information about the libc-commits mailing list