[libcxx-commits] [libcxx] [libc++] Implement adjacent_view (PR #165089)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Dec 5 12:03:56 PST 2025
================
@@ -0,0 +1,122 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// constexpr auto size() requires sized_range<View>
+// constexpr auto size() const requires sized_range<const View>
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "../range_adaptor_types.h"
+
+int buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
+struct View : std::ranges::view_base {
+ std::size_t size_ = 0;
+ constexpr View(std::size_t s) : size_(s) {}
+ constexpr auto begin() const { return buffer; }
+ constexpr auto end() const { return buffer + size_; }
+};
+
+struct SizedNonConst : std::ranges::view_base {
+ using iterator = forward_iterator<int*>;
+ std::size_t size_ = 0;
+ constexpr SizedNonConst(std::size_t s) : size_(s) {}
+ constexpr auto begin() const { return iterator{buffer}; }
+ constexpr auto end() const { return iterator{buffer + size_}; }
+ constexpr std::size_t size() { return size_; }
+};
+
+struct StrangeSizeView : std::ranges::view_base {
+ constexpr auto begin() const { return buffer; }
+ constexpr auto end() const { return buffer + 8; }
+
+ constexpr auto size() { return 5; }
+ constexpr auto size() const { return 6; }
+};
+
+// Test with different values of N for a sized view
+template <std::size_t N>
+constexpr void test_sized_view() {
+ std::ranges::adjacent_view<View, N> v(View(8));
+ static_assert(std::ranges::sized_range<decltype(v)>);
+ static_assert(std::ranges::sized_range<const decltype(v)>);
+
+ auto expected_size = 8 - (N - 1);
+ assert(v.size() == expected_size);
+ assert(std::as_const(v).size() == expected_size);
+}
+
+// Test with different values of N for a non-const sized view
+template <std::size_t N>
+constexpr void test_nonconst_sized() {
----------------
ldionne wrote:
Same comment as elsewhere: suggest inlining with a scope.
https://github.com/llvm/llvm-project/pull/165089
More information about the libcxx-commits
mailing list