[libcxx-commits] [libcxx] [libc++][ranges] P2164R9: Implements `views::enumerate` (PR #73617)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Sep 27 10:06:42 PDT 2025
================
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++23
+
+// <ranges>
+
+// class enumerate_view
+
+// class enumerate_view::iterator
+
+// constexpr auto operator[](difference_type n) const
+// requires random_access_range<Base>;
+
+#include <array>
+#include <cassert>
+#include <ranges>
+#include <tuple>
+
+#include "test_iterators.h"
+
+template <class T, class U>
+concept HasSubscriptOperator = requires(T t, U u) { t[u]; };
+
+template <class BaseRange>
+using EnumerateIterator = std::ranges::iterator_t<std::ranges::enumerate_view<BaseRange>>;
+
+using Subrange = std::ranges::subrange<int*>;
+static_assert(HasSubscriptOperator<EnumerateIterator<Subrange>, int>);
+
+using BidirectionalRange = std::ranges::subrange<bidirectional_iterator<int*>>;
+static_assert(!HasSubscriptOperator<EnumerateIterator<BidirectionalRange>, int>);
+
+constexpr bool test() {
+ // Reference
+ {
+ std::array ts = {90, 1, 2, 84};
+ auto view = ts | std::views::enumerate;
+ auto it = view.begin();
+
+ using DifferenceT = std::iter_difference_t<std::iter_difference_t<decltype(it)>>;
----------------
frederick-vs-ja wrote:
Why is `std::iter_difference_t` doubly used? Ditto below.
https://github.com/llvm/llvm-project/pull/73617
More information about the libcxx-commits
mailing list