[libcxx-commits] [libcxx] [libc++][ranges] P2116R9: Implements `views::enumerate` (PR #73617)
Konstantin Varlamov via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 5 20:47:27 PST 2023
================
@@ -0,0 +1,128 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// std::views::enumerate;
+
+#include <algorithm>
+#include <cassert>
+#include <ranges>
+#include <string_view>
+
+#include "types.h"
+
+// Concepts
+
+template <class View, class T>
+concept CanBePiped = requires(View&& view, T&& t) {
+ { std::forward<View>(view) | std::forward<T>(t) };
+};
+
+template <class Range>
+concept CanEnumerate = requires(Range&& range) { std::views::enumerate(std::forward<Range>(range)); };
+
+// Helpers
+
+struct ImmovableReference {
+ ImmovableReference(ImmovableReference&&) = delete;
+
+ operator int();
+};
+
+struct IteratorWithImmovableReferences {
+ using value_type = int;
+ using difference_type = std::ptrdiff_t;
+
+ ImmovableReference operator*() const;
+ IteratorWithImmovableReferences& operator++();
+ void operator++(int);
+ bool operator==(std::default_sentinel_t) const;
+};
+
+static_assert(std::input_iterator<IteratorWithImmovableReferences>);
+
+using NonEnumeratableRangeWithImmmovabaleReferences =
+ std::ranges::subrange<IteratorWithImmovableReferences, std::default_sentinel_t>;
+
+static_assert(std::ranges::input_range<NonEnumeratableRangeWithImmmovabaleReferences>);
----------------
var-const wrote:
Can you also check that
```cpp
static_assert(!std::move_constructible<ranges::range_reference_t< NonEnumeratableRangeWithImmmovabaleReferences >>);
static_assert(!std::move_constructible<ranges::range_rvalue_reference_t< NonEnumeratableRangeWithImmmovabaleReferences >>);
```
? That makes it clearer that we're testing the constraints of the `__range_with_movable_references` concept.
Ideally, we'd also check the case where only one of `range_reference_t` and `range_rvalue_reference_t` is false, but this might be not worth the trouble.
https://github.com/llvm/llvm-project/pull/73617
More information about the libcxx-commits
mailing list