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

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


================
@@ -0,0 +1,104 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// std::enumerate_view::<iterator>::difference_type;
+// std::enumerate_view::<iterator>::value_type;
+// std::enumerate_view::<iterator>::iterator_category;
+// std::enumerate_view::<iterator>::iterator_concept;
+
+#include <ranges>
+
+#include <type_traits>
+#include "test_iterators.h"
+#include "../types.h"
+
+template <typename T>
+concept HasIteratorCategory = requires { typename T::iterator_category; };
+
+template <class Iterator>
+using EnumerateViewFor = std::ranges::enumerate_view< MinimalView<Iterator, sentinel_wrapper<Iterator>>>;
+
+template <class Iterator>
+using EnumerateIteratorFor = std::ranges::iterator_t<EnumerateViewFor<Iterator>>;
+
+struct ForwardIteratorWithInputCategory {
+  using difference_type   = int;
+  using value_type        = int;
+  using iterator_category = std::input_iterator_tag;
+  using iterator_concept  = std::forward_iterator_tag;
+  ForwardIteratorWithInputCategory();
+  ForwardIteratorWithInputCategory& operator++();
+  ForwardIteratorWithInputCategory operator++(int);
+  int& operator*() const;
+  friend bool operator==(ForwardIteratorWithInputCategory, ForwardIteratorWithInputCategory);
+};
+static_assert(std::forward_iterator<ForwardIteratorWithInputCategory>);
+
+constexpr void test() {
+  // Check that value_type is range_value_t and difference_type is range_difference_t
+  {
+    auto test = []<class Iterator> {
+      using EnumerateView     = EnumerateViewFor<Iterator>;
+      using EnumerateIterator = EnumerateIteratorFor<Iterator>;
+      static_assert(std::is_same_v<typename EnumerateIterator::value_type, std::ranges::range_value_t<EnumerateView>>);
+      static_assert(
+          std::is_same_v<typename EnumerateIterator::difference_type, std::ranges::range_difference_t<EnumerateView>>);
+    };
+    test.operator()<cpp17_input_iterator<int*>>();
+    test.operator()<cpp20_input_iterator<int*>>();
+    test.operator()<forward_iterator<int*>>();
+    test.operator()<bidirectional_iterator<int*>>();
+    test.operator()<random_access_iterator<int*>>();
+    test.operator()<contiguous_iterator<int*>>();
+    test.operator()<int*>();
----------------
cjdb wrote:

Please turn `test` into a function template instead of a lambda template. It lends itself to a nicer syntax.

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


More information about the flang-commits mailing list