[libcxx-commits] [libcxx] [libcxx] cbegin should always return a constant iterator (PR #99915)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Sep 9 07:27:32 PDT 2024
================
@@ -0,0 +1,266 @@
+// -*- 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_CONST_ACCESS_H
+#define _LIBCPP___RANGES_CONST_ACCESS_H
+
+#include <__iterator/const_iterator.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/enable_borrowed_range.h>
+#include <__ranges/rbegin.h>
+#include <__ranges/rend.h>
+#include <__type_traits/is_reference.h>
+#include <__type_traits/remove_cv.h>
+#include <__type_traits/remove_reference.h>
+#include <__utility/declval.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 20
+
+// [range.const]
+# if _LIBCPP_STD_VER >= 23
+namespace ranges {
+template <input_range _Rp>
+_LIBCPP_HIDE_FROM_ABI constexpr auto& __possibly_const_range(_Rp& __rng) noexcept {
+ if constexpr (constant_range<const _Rp> && !constant_range<_Rp>) {
+ return const_cast<const _Rp&>(__rng);
+ } else {
+ return __rng;
+ }
+}
+} // namespace ranges
+# endif // _LIBCPP_STD_VER >= 23
+
+// [range.access.cbegin]
+namespace ranges {
+
+template <class _Type>
+concept __const_accessible_range = (!is_rvalue_reference_v<_Type&&> || enable_borrowed_range<remove_cv_t<_Type>>);
+
+namespace __cbegin {
+struct __fn {
+# if _LIBCPP_STD_VER >= 23
+ template <class _Rng>
+ using _UType = decltype(ranges::begin(ranges::__possibly_const_range(std::declval<_Rng&>())));
+
+ template <__const_accessible_range _Rng>
----------------
ldionne wrote:
Do you need to catch http://eel.is/c++draft/range.access.cbegin#1.1
> If E is an rvalue and enable_borrowed_range<remove_cv_t<T>> is false, ranges::cbegin(E) is ill-formed[.](http://eel.is/c++draft/range.access.cbegin#1.1.sentence-1)
explicitly?
https://github.com/llvm/llvm-project/pull/99915
More information about the libcxx-commits
mailing list