2021-04-11 05:08:32 +00:00
|
|
|
// -*- C++ -*-
|
2021-11-17 21:25:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2021-04-11 05:08:32 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2022-11-25 15:25:10 +00:00
|
|
|
|
2021-04-11 05:08:32 +00:00
|
|
|
#ifndef _LIBCPP___RANGES_ACCESS_H
|
|
|
|
#define _LIBCPP___RANGES_ACCESS_H
|
|
|
|
|
2021-12-23 21:53:48 +00:00
|
|
|
#include <__concepts/class_or_enum.h>
|
2021-04-11 05:08:32 +00:00
|
|
|
#include <__config>
|
|
|
|
#include <__iterator/concepts.h>
|
2021-06-12 06:13:44 +00:00
|
|
|
#include <__iterator/readable_traits.h>
|
2021-04-11 05:08:32 +00:00
|
|
|
#include <__ranges/enable_borrowed_range.h>
|
2022-12-20 23:07:17 +00:00
|
|
|
#include <__type_traits/decay.h>
|
|
|
|
#include <__type_traits/is_reference.h>
|
|
|
|
#include <__type_traits/remove_cvref.h>
|
|
|
|
#include <__type_traits/remove_reference.h>
|
2021-12-14 00:21:38 +00:00
|
|
|
#include <__utility/auto_cast.h>
|
2022-12-20 23:07:17 +00:00
|
|
|
#include <__utility/declval.h>
|
|
|
|
#include <cstddef>
|
2021-04-11 05:08:32 +00:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 01:16:40 +00:00
|
|
|
# pragma GCC system_header
|
2021-04-11 05:08:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2023-02-13 23:56:09 +00:00
|
|
|
#if _LIBCPP_STD_VER >= 20
|
2021-04-11 05:08:32 +00:00
|
|
|
|
|
|
|
namespace ranges {
|
|
|
|
template <class _Tp>
|
2021-12-23 19:51:24 +00:00
|
|
|
concept __can_borrow = is_lvalue_reference_v<_Tp> || enable_borrowed_range<remove_cvref_t<_Tp>>;
|
2021-04-11 05:08:32 +00:00
|
|
|
} // namespace ranges
|
|
|
|
|
|
|
|
// [range.access.begin]
|
2021-12-23 19:51:24 +00:00
|
|
|
|
|
|
|
namespace ranges {
|
|
|
|
namespace __begin {
|
2021-04-11 05:08:32 +00:00
|
|
|
template <class _Tp>
|
2021-12-23 21:53:48 +00:00
|
|
|
concept __member_begin = __can_borrow<_Tp> && __workaround_52970<_Tp> && requires(_Tp&& __t) {
|
2021-12-14 00:21:38 +00:00
|
|
|
{ _LIBCPP_AUTO_CAST(__t.begin()) } -> input_or_output_iterator;
|
2021-04-11 05:08:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void begin(auto&) = delete;
|
|
|
|
void begin(const auto&) = delete;
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
concept __unqualified_begin =
|
2021-12-23 19:51:24 +00:00
|
|
|
!__member_begin<_Tp> && __can_borrow<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
|
2021-12-14 00:21:38 +00:00
|
|
|
{ _LIBCPP_AUTO_CAST(begin(__t)) } -> input_or_output_iterator;
|
2021-04-11 05:08:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct __fn {
|
|
|
|
template <class _Tp>
|
2022-02-04 02:41:30 +00:00
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp (&__t)[]) const noexcept
|
2022-02-27 17:41:22 +00:00
|
|
|
requires(sizeof(_Tp) >= 0) // Disallow incomplete element types.
|
[libc++] [ranges] Remove the static_assert from ranges::begin and ranges::end.
As discussed with ldionne. The problem with this static_assert
is that it makes ranges::begin a pitfall for anyone ever to use
inside a constraint or decltype. Many Ranges things, such as ranges::size,
are specified as "Does X if X is well-formed, or else Y if Y is well-formed,
or else `ranges::end(t) - ranges::begin(t)` if that is well-formed, or else..."
And if there's a static_assert hidden inside `ranges::begin(t)`, then you get
a hard error as soon as you ask the question -- even if the answer would have
been "no, that's not well-formed"!
Constraining on `requires { t + 0; }` or `requires { t + N; }` is verboten
because of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103700 . For ranges::begin,
we can just decay to a pointer even in the incomplete-type case. For ranges::end,
we can safely constrain on `sizeof(*t)`. Yes, this means that an array of incomplete
type has a `ranges::begin` but no `ranges::end`... just like an unbounded array of
complete type. This is a valid manifestation of IFNDR.
All of the new libcxx/test/std/ cases are mandatory behavior, as far as I'm aware.
Tests for the IFNDR cases in ranges::begin and ranges::end remain in `libcxx/test/libcxx/`.
The similar tests for ranges::empty and ranges::data were simply wrong, AFAIK.
Differential Revision: https://reviews.llvm.org/D115838
2021-12-16 03:10:34 +00:00
|
|
|
{
|
2022-02-04 02:41:30 +00:00
|
|
|
return __t + 0;
|
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2022-02-04 02:41:30 +00:00
|
|
|
template <class _Tp, size_t _Np>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp (&__t)[_Np]) const noexcept
|
2022-02-27 17:41:22 +00:00
|
|
|
requires(sizeof(_Tp) >= 0) // Disallow incomplete element types.
|
2022-02-04 02:41:30 +00:00
|
|
|
{
|
|
|
|
return __t + 0;
|
2021-04-11 05:08:32 +00:00
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2021-04-11 05:08:32 +00:00
|
|
|
template <class _Tp>
|
2021-12-23 19:51:24 +00:00
|
|
|
requires __member_begin<_Tp>
|
2021-07-19 16:34:56 +00:00
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
|
2021-12-23 19:51:24 +00:00
|
|
|
noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.begin()))) {
|
2021-12-14 00:21:38 +00:00
|
|
|
return _LIBCPP_AUTO_CAST(__t.begin());
|
2021-04-11 05:08:32 +00:00
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2021-04-11 05:08:32 +00:00
|
|
|
template <class _Tp>
|
2021-12-23 19:51:24 +00:00
|
|
|
requires __unqualified_begin<_Tp>
|
2021-07-19 16:34:56 +00:00
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
|
2021-12-23 19:51:24 +00:00
|
|
|
noexcept(noexcept(_LIBCPP_AUTO_CAST(begin(__t)))) {
|
2021-12-14 00:21:38 +00:00
|
|
|
return _LIBCPP_AUTO_CAST(begin(__t));
|
2021-04-11 05:08:32 +00:00
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2021-04-11 05:08:32 +00:00
|
|
|
void operator()(auto&&) const = delete;
|
|
|
|
};
|
2022-02-01 17:11:49 +00:00
|
|
|
} // namespace __begin
|
2021-04-11 05:08:32 +00:00
|
|
|
|
2021-12-23 19:51:24 +00:00
|
|
|
inline namespace __cpo {
|
|
|
|
inline constexpr auto begin = __begin::__fn{};
|
|
|
|
} // namespace __cpo
|
|
|
|
} // namespace ranges
|
2021-04-11 05:08:32 +00:00
|
|
|
|
2021-12-23 19:51:24 +00:00
|
|
|
// [range.range]
|
|
|
|
|
|
|
|
namespace ranges {
|
2021-04-11 05:08:32 +00:00
|
|
|
template <class _Tp>
|
[libc++][NFC] Qualify declval
While it's not necessary to qualify calls to `declval` it makes error messages very crypric if the declaration isn't reachable anymore
For example:
```
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<long, long>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>, std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:280:81: error: no type named 'type' in 'std::common_type<std::chrono::duration<long, std::ratio<3600, 1>>>'
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:308:54: note: in instantiation of template class 'std::chrono::duration<long, std::ratio<3600, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:53:66: error: no type named 'type' in 'std::common_type<int, int>'
typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:107:14: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>, std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
: public common_type<_Tp, _Tp> {};
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:279:58: note: in instantiation of template class 'std::common_type<std::chrono::duration<int, std::ratio<86400, 1>>>' requested here
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__chrono/duration.h:309:55: note: in instantiation of template class 'std::chrono::duration<int, std::ratio<86400, 1>>' requested here
typedef duration< int, ratio_multiply<ratio<7>, days::period>> weeks;
^
19 similar errors omitted
```
changes with qualification added to:
```
While building module 'std' imported from /home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:
In file included from <module-includes>:17:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/math.h:309:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/limits:107:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/type_traits:432:
In file included from /home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_reference.h:13:
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__type_traits/common_type.h:28:43: error: declaration of 'declval' must be imported from module 'std.utility.__utility.declval' before it is required
using __cond_type = decltype(false ? std::declval<_Tp>() : std::declval<_Up>());
^
/home/nikolask/llvm-projects/libcxx/build/include/c++/v1/__utility/declval.h:30:34: note: declaration here is not visible
decltype(std::__declval<_Tp>(0)) declval() _NOEXCEPT;
^
/home/nikolask/llvm-projects/libcxx/libcxx/test/std/utilities/meta/meta.trans/meta.trans.other/common_type.pass.cpp:13:10: fatal error: could not build module 'std'
#include <functional>
~~~~~~~~^
2 errors generated.
```
Reviewed By: ldionne, Mordante, #libc
Spies: libcxx-commits
Differential Revision: https://reviews.llvm.org/D130854
2022-07-31 19:54:25 +00:00
|
|
|
using iterator_t = decltype(ranges::begin(std::declval<_Tp&>()));
|
2021-04-11 05:08:32 +00:00
|
|
|
} // namespace ranges
|
|
|
|
|
|
|
|
// [range.access.end]
|
2021-12-23 19:51:24 +00:00
|
|
|
|
|
|
|
namespace ranges {
|
|
|
|
namespace __end {
|
2021-04-11 05:08:32 +00:00
|
|
|
template <class _Tp>
|
2021-12-23 21:53:48 +00:00
|
|
|
concept __member_end = __can_borrow<_Tp> && __workaround_52970<_Tp> && requires(_Tp&& __t) {
|
2021-04-11 05:08:32 +00:00
|
|
|
typename iterator_t<_Tp>;
|
2021-12-23 19:51:24 +00:00
|
|
|
{ _LIBCPP_AUTO_CAST(__t.end()) } -> sentinel_for<iterator_t<_Tp>>;
|
2023-12-18 19:01:33 +00:00
|
|
|
};
|
|
|
|
|
2021-04-11 05:08:32 +00:00
|
|
|
void end(auto&) = delete;
|
|
|
|
void end(const auto&) = delete;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2021-04-11 05:08:32 +00:00
|
|
|
template <class _Tp>
|
|
|
|
concept __unqualified_end =
|
|
|
|
!__member_end<_Tp> && __can_borrow<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
|
|
|
|
typename iterator_t<_Tp>;
|
2021-12-23 19:51:24 +00:00
|
|
|
{ _LIBCPP_AUTO_CAST(end(__t)) } -> sentinel_for<iterator_t<_Tp>>;
|
2021-04-11 05:08:32 +00:00
|
|
|
};
|
|
|
|
|
2022-03-22 19:34:48 +00:00
|
|
|
struct __fn {
|
2021-04-11 05:08:32 +00:00
|
|
|
template <class _Tp, size_t _Np>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp (&__t)[_Np]) const noexcept
|
|
|
|
requires(sizeof(_Tp) >= 0) // Disallow incomplete element types.
|
2023-12-18 19:01:33 +00:00
|
|
|
{
|
2021-04-11 05:08:32 +00:00
|
|
|
return __t + _Np;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2021-04-11 05:08:32 +00:00
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
requires __member_end<_Tp>
|
2021-12-23 19:51:24 +00:00
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
|
|
|
|
noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.end()))) {
|
|
|
|
return _LIBCPP_AUTO_CAST(__t.end());
|
|
|
|
}
|
2021-04-11 05:08:32 +00:00
|
|
|
|
|
|
|
template <class _Tp>
|
2021-12-23 19:51:24 +00:00
|
|
|
requires __unqualified_end<_Tp>
|
2021-07-19 16:34:56 +00:00
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
|
2021-12-23 19:51:24 +00:00
|
|
|
noexcept(noexcept(_LIBCPP_AUTO_CAST(end(__t)))) {
|
2021-12-14 00:21:38 +00:00
|
|
|
return _LIBCPP_AUTO_CAST(end(__t));
|
2021-04-11 05:08:32 +00:00
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2021-04-11 05:08:32 +00:00
|
|
|
void operator()(auto&&) const = delete;
|
|
|
|
};
|
2022-02-01 17:11:49 +00:00
|
|
|
} // namespace __end
|
2021-04-11 05:08:32 +00:00
|
|
|
|
2021-12-23 19:51:24 +00:00
|
|
|
inline namespace __cpo {
|
2021-04-11 05:08:32 +00:00
|
|
|
inline constexpr auto end = __end::__fn{};
|
2021-12-23 19:51:24 +00:00
|
|
|
} // namespace __cpo
|
|
|
|
} // namespace ranges
|
|
|
|
|
|
|
|
// [range.access.cbegin]
|
2021-04-11 05:08:32 +00:00
|
|
|
|
2021-12-23 19:51:24 +00:00
|
|
|
namespace ranges {
|
|
|
|
namespace __cbegin {
|
2021-04-11 05:08:32 +00:00
|
|
|
struct __fn {
|
|
|
|
template <class _Tp>
|
2022-01-11 04:37:01 +00:00
|
|
|
requires is_lvalue_reference_v<_Tp&&>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
|
|
|
|
noexcept(noexcept(ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t))))
|
|
|
|
-> decltype(ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t))) {
|
|
|
|
return ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t));
|
|
|
|
}
|
2021-04-11 05:08:32 +00:00
|
|
|
|
|
|
|
template <class _Tp>
|
2021-12-23 03:36:42 +00:00
|
|
|
requires is_rvalue_reference_v<_Tp&&>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
|
|
|
|
noexcept(noexcept(ranges::begin(static_cast<const _Tp&&>(__t))))
|
|
|
|
-> decltype(ranges::begin(static_cast<const _Tp&&>(__t))) {
|
|
|
|
return ranges::begin(static_cast<const _Tp&&>(__t));
|
|
|
|
}
|
2021-04-11 05:08:32 +00:00
|
|
|
};
|
2022-02-01 17:11:49 +00:00
|
|
|
} // namespace __cbegin
|
2021-04-11 05:08:32 +00:00
|
|
|
|
2021-12-23 19:51:24 +00:00
|
|
|
inline namespace __cpo {
|
2021-04-11 05:08:32 +00:00
|
|
|
inline constexpr auto cbegin = __cbegin::__fn{};
|
2021-12-23 19:51:24 +00:00
|
|
|
} // namespace __cpo
|
|
|
|
} // namespace ranges
|
2021-04-11 05:08:32 +00:00
|
|
|
|
2021-12-23 19:51:24 +00:00
|
|
|
// [range.access.cend]
|
|
|
|
|
|
|
|
namespace ranges {
|
|
|
|
namespace __cend {
|
2021-04-11 05:08:32 +00:00
|
|
|
struct __fn {
|
|
|
|
template <class _Tp>
|
2022-01-11 04:37:01 +00:00
|
|
|
requires is_lvalue_reference_v<_Tp&&>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
|
|
|
|
noexcept(noexcept(ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t))))
|
|
|
|
-> decltype(ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t))) {
|
|
|
|
return ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t));
|
|
|
|
}
|
2021-04-11 05:08:32 +00:00
|
|
|
|
|
|
|
template <class _Tp>
|
2021-12-23 03:36:42 +00:00
|
|
|
requires is_rvalue_reference_v<_Tp&&>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
|
|
|
|
noexcept(noexcept(ranges::end(static_cast<const _Tp&&>(__t))))
|
|
|
|
-> decltype(ranges::end(static_cast<const _Tp&&>(__t))) {
|
|
|
|
return ranges::end(static_cast<const _Tp&&>(__t));
|
|
|
|
}
|
2021-04-11 05:08:32 +00:00
|
|
|
};
|
2022-02-01 17:11:49 +00:00
|
|
|
} // namespace __cend
|
2021-04-11 05:08:32 +00:00
|
|
|
|
2021-12-23 19:51:24 +00:00
|
|
|
inline namespace __cpo {
|
2021-04-11 05:08:32 +00:00
|
|
|
inline constexpr auto cend = __cend::__fn{};
|
2021-12-23 19:51:24 +00:00
|
|
|
} // namespace __cpo
|
|
|
|
} // namespace ranges
|
2021-04-11 05:08:32 +00:00
|
|
|
|
2023-02-13 23:56:09 +00:00
|
|
|
#endif // _LIBCPP_STD_VER >= 20
|
2021-04-11 05:08:32 +00:00
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
#endif // _LIBCPP___RANGES_ACCESS_H
|