[libcxx-commits] [libcxx] [libc++] The rest of P2278R4 (cdata, as_const_view, span changes) (PR #102301)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 7 10:33:51 PDT 2024


================
@@ -0,0 +1,204 @@
+// -*- 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_AS_CONST_VIEW_H
+#define _LIBCPP___RANGES_AS_CONST_VIEW_H
+
+#include <__concepts/constructible.h>
+#include <__iterator/concepts.h>
+#include <__ranges/all.h>
+#include <__ranges/concepts.h>
+#include <__ranges/const_access.h>
+#include <__ranges/empty_view.h>
+#include <__ranges/range_adaptor.h>
+#include <__ranges/ref_view.h>
+#include <__ranges/size.h>
+#include <__ranges/view_interface.h>
+#include <__type_traits/is_specialization.h>
+#include <__utility/auto_cast.h>
+#include <__utility/move.h>
+#include <cstddef>
+#include <span>
+
+#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 >= 23
+
+namespace ranges {
+template <input_range _View>
+  requires view<_View>
+class as_const_view : public view_interface<as_const_view<_View>> {
+  _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
+
+public:
+  _LIBCPP_HIDE_FROM_ABI as_const_view()
+    requires default_initializable<_View>
+  = default;
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit as_const_view(_View __base) : __base_(std::move(__base)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+    requires copy_constructible<_View>
+  {
+    return __base_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
+    requires(!__simple_view<_View>)
+  {
+    return ranges::cbegin(__base_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+    requires range<const _View>
+  {
+    return ranges::cbegin(__base_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto end()
+    requires(!__simple_view<_View>)
+  {
+    return ranges::cend(__base_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+    requires range<const _View>
+  {
+    return ranges::cend(__base_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+    requires sized_range<_View>
+  {
+    return ranges::size(__base_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+    requires sized_range<const _View>
+  {
+    return ranges::size(__base_);
+  }
+};
+
+template <class _Range>
+as_const_view(_Range&&) -> as_const_view<views::all_t<_Range>>;
+
+template <class _Tp>
+inline constexpr bool enable_borrowed_range<as_const_view<_Tp>> = enable_borrowed_range<_Tp>;
+
+namespace views {
+namespace __as_const {
+
+template <class _Tp>
+inline constexpr bool __is_span_v = false; // true if and only if _Tp is a specialization of span
+template <class _Tp, size_t _Extent>
+inline constexpr bool __is_span_v<span<_Tp, _Extent>> = true;
+
+template <class _UType>
+struct __xtype {
+  using type = void;
+};
+template <class _XType>
+struct __xtype<empty_view<_XType>> {
+  using type = _XType;
+};
+template <class _XType, size_t _Extent>
+struct __xtype<span<_XType, _Extent>> {
+  using type                       = _XType;
+  constexpr static size_t __extent = _Extent;
+};
+template <class _XType>
+struct __xtype<ref_view<_XType>> {
+  using type = _XType;
+};
+
+struct __fn : __range_adaptor_closure<__fn> {
+  // implementation strategy taken from Microsoft's STL
+  enum class __strategy {
+    __already_const,
+    __empty_view,
+    __span,
+    __ref_view,
----------------
frederick-vs-ja wrote:

[LWG3811](https://cplusplus.github.io/LWG/issue3811) and [LWG3850](https://cplusplus.github.io/LWG/issue3850) are completed here and thus the entries should be updated:
https://github.com/llvm/llvm-project/blob/445023f1739d3764f1dae6e6b7b0602328198a1e/libcxx/docs/Status/Cxx23Issues.csv#L257
https://github.com/llvm/llvm-project/blob/445023f1739d3764f1dae6e6b7b0602328198a1e/libcxx/docs/Status/Cxx23Issues.csv#L272


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


More information about the libcxx-commits mailing list