[libcxx-commits] [libcxx] WIP [libc++][ranges] `std::views::as_input` (PR #151449)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 31 04:37:13 PDT 2026


================
@@ -0,0 +1,226 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___RANGES_TO_INPUT_VIEW_H
+#define _LIBCPP___RANGES_TO_INPUT_VIEW_H
+
+#include <__concepts/constructible.h>
+#include <__concepts/convertible_to.h>
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__iterator/iter_move.h>
+#include <__iterator/iter_swap.h>
+#include <__iterator/iterator_traits.h>
+#include <__ranges/access.h>
+#include <__ranges/all.h>
+#include <__ranges/concepts.h>
+#include <__ranges/enable_borrowed_range.h>
+#include <__ranges/range_adaptor.h>
+#include <__ranges/size.h>
+#include <__ranges/view_interface.h>
+#include <__type_traits/maybe_const.h>
+#include <__utility/forward.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 26
+
+namespace ranges {
+
+// [range.to.input.view]
+
+template <input_range _View>
+  requires view<_View>
+class as_input_view : public view_interface<as_input_view<_View>> {
+  _View __base_ = _View(); // exposition only
+
+  // [range.to.input.iterator], class template as_input_view::iterator
+  template <bool _Const>
+  class iterator; // exposition only
+
+public:
+  _LIBCPP_HIDE_FROM_ABI as_input_view()
+    requires default_initializable<_View>
+  = default;
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit as_input_view(_View __base) : __base_(std::move(__base)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+    requires copy_constructible<_View>
+  {
+    return __base_;
+  }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
+
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
+    requires(!__simple_view<_View>)
+  {
+    return iterator<false>{ranges::begin(__base_)};
+  }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+    requires range<const _View>
+  {
+    return iterator<true>{ranges::begin(__base_)};
+  }
+
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end()
+    requires(!__simple_view<_View>)
+  {
+    return ranges::end(__base_);
+  }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+    requires range<const _View>
+  {
+    return ranges::end(__base_);
+  }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+    requires sized_range<_View>
+  {
+    return ranges::size(__base_);
+  }
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+    requires sized_range<const _View>
+  {
+    return ranges::size(__base_);
+  }
+
+  // TODO: Implement when P2846R6 is available.
+  // constexpr auto reserve_hint()
+  //   requires approximately_sized_range<_View>
+  // {
+  //   return ranges::reserve_hint(__base_);
+  // }
+  // constexpr auto reserve_hint() const
+  //   requires approximately_sized_range<const _View>
+  // {
+  //   return ranges::reserve_hint(__base_);
+  // }
----------------
Zingam wrote:

I'll keep these for now as a reference in case we manage to land https://github.com/llvm/llvm-project/pull/206385 first.

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


More information about the libcxx-commits mailing list