[libcxx] [clang-tools-extra] [flang] [compiler-rt] [llvm] [clang] [lld] [libc++][ranges] P2116R9: Implements `views::enumerate` (PR #73617)

Christopher Di Bella via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 2 14:17:05 PST 2024


================
@@ -0,0 +1,95 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <ranges>
+
+// class enumerate_view
+
+// class enumerate_view::iterator
+
+// constexpr difference_type index() const noexcept;
+
+#include <array>
+#include <cassert>
+#include <concepts>
+#include <cstdint>
+#include <utility>
+#include <tuple>
+
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../types.h"
+
+template <class Iterator, class ValueType = int, class Sentinel = sentinel_wrapper<Iterator>>
+constexpr void test() {
+  using View          = MinimalView<Iterator, Sentinel>;
+  using EnumerateView = std::ranges::enumerate_view<View>;
+
+  std::array array{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+
+  View view{Iterator(array.begin()), Sentinel(Iterator(array.end()))};
+  EnumerateView ev(std::move(view));
+
+  {
+    auto it = ev.begin();
+    ASSERT_NOEXCEPT(it.index());
+
+    static_assert(std::same_as<typename decltype(it)::difference_type, decltype(it.index())>);
+    for (std::size_t index = 0; index < array.size(); ++index) {
+      assert(std::cmp_equal(index, it.index()));
+
+      ++it;
+    }
----------------
cjdb wrote:

Please hand-roll.

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


More information about the llvm-commits mailing list