[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:31 PDT 2024
================
@@ -0,0 +1,351 @@
+// -*- 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___ITERATOR_CONST_ITERATOR_H
+#define _LIBCPP___ITERATOR_CONST_ITERATOR_H
+
+#include <__compare/three_way_comparable.h>
+#include <__concepts/common_with.h>
+#include <__concepts/convertible_to.h>
+#include <__concepts/different_from.h>
+#include <__concepts/same_as.h>
+#include <__concepts/semiregular.h>
+#include <__concepts/totally_ordered.h>
+#include <__iterator/concepts.h>
+#include <__iterator/incrementable_traits.h>
+#include <__iterator/iter_move.h>
+#include <__iterator/iterator_traits.h>
+#include <__memory/addressof.h>
+#include <__memory/pointer_traits.h>
+#include <__type_traits/common_reference.h>
+#include <__type_traits/common_type.h>
+#include <__type_traits/conditional.h>
+#include <__type_traits/integral_constant.h>
+#include <__type_traits/is_reference.h>
+#include <__type_traits/is_specialization.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 >= 23
+
+template <indirectly_readable _Iter>
+using iter_const_reference_t = common_reference_t<const iter_value_t<_Iter>&&, iter_reference_t<_Iter>>;
+
+template <class _Iter>
+concept __constant_iterator = input_iterator<_Iter> && same_as<iter_const_reference_t<_Iter>, iter_reference_t<_Iter>>;
+
+template <input_iterator _Iter>
+class basic_const_iterator;
+
+template <input_iterator _Iter>
+using const_iterator = conditional_t<__constant_iterator<_Iter>, _Iter, basic_const_iterator<_Iter>>;
+
+// This doesn't use `conditional_t` to avoid instantiating const_iterator<_Sent> when _Sent is not an input_iterator.
+template <class _Sent>
+struct __const_sentinel_impl {
+ using type = _Sent;
+};
+template <class _Sent>
+ requires input_iterator<_Sent>
+struct __const_sentinel_impl<_Sent> {
+ using type = const_iterator<_Sent>;
+};
+template <semiregular _Sent>
+using const_sentinel = __const_sentinel_impl<_Sent>::type;
+
+template <class _Iter>
+concept __not_a_const_iterator = !__is_specialization_v<_Iter, basic_const_iterator>;
+
+template <indirectly_readable _Iter>
+using __iter_const_rvalue_reference_t = common_reference_t<const iter_value_t<_Iter>&&, iter_rvalue_reference_t<_Iter>>;
+
+template <class _Iter>
+struct __basic_const_iterator_concept {
+ // clang-format off
+ using iterator_concept =
+ conditional_t<contiguous_iterator<_Iter>,
+ contiguous_iterator_tag,
+ conditional_t<random_access_iterator<_Iter>,
+ random_access_iterator_tag,
+ conditional_t<bidirectional_iterator<_Iter>,
+ bidirectional_iterator_tag,
+ conditional_t<forward_iterator<_Iter>,
+ forward_iterator_tag,
+ // else
+ input_iterator_tag>>>>;
+ // clang-format on
+};
+
+template <class _Iter>
+struct __basic_const_iterator_category : __basic_const_iterator_concept<_Iter> {};
+template <forward_iterator _Iter>
+struct __basic_const_iterator_category<_Iter> : __basic_const_iterator_concept<_Iter> {
+ using iterator_category = std::iterator_traits<_Iter>::iterator_category;
+};
+
+template <input_iterator _Iter>
+class _LIBCPP_TEMPLATE_VIS basic_const_iterator : public __basic_const_iterator_category<_Iter> {
+ _Iter __current_ = _Iter();
+
+ using __reference = iter_const_reference_t<_Iter>;
+ using __rvalue_reference = __iter_const_rvalue_reference_t<_Iter>;
+
+public:
+ using value_type = iter_value_t<_Iter>;
+ using difference_type = iter_difference_t<_Iter>;
+
+ _LIBCPP_HIDE_FROM_ABI basic_const_iterator()
+ requires default_initializable<_Iter>
+ = default;
+
+ _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator(_Iter __cur) : __current_(std::move(__cur)) {}
----------------
ldionne wrote:
Do you have tests to ensure that these constructors are not explicit? I.e. in your tests, do you use a syntax that would fail if these constructors were explicit? An easy way to tell is to make them `explicit` and see what happens :)
https://github.com/llvm/llvm-project/pull/99915
More information about the libcxx-commits
mailing list