gcc/libstdc++-v3/include/bits/ranges_cmp.h
Jonathan Wakely 160061ac10 libstdc++: Introduce new headers for C++20 ranges components
This introduces two new headers:

<bits/ranges_base.h> defines the minimal components needed
for using C++20 ranges (customization point objects such as
std::ranges::begin, concepts such as std::ranges::range, etc.)

<bits/ranges_util.h> includes <bits/ranges_base.h> and additionally
defines subrange, which is needed by <bits/ranges_algo.h>.

Most of the content of <bits/ranges_base.h> was previously defined in
<bits/range_access.h>, but a few pieces were only defined in <ranges>.
This meant the entire <ranges> header was needed in <algorithm> and
<memory>, even though they don't use all the range adaptors.

By moving the ranges components out of <bits/range_access.h> that file
is left defining just the contents of [iterator.range] i.e. std::begin,
std::end, std::size etc. and not C++20 ranges components.

For consistency with other C++20 ranges headers, <bits/range_cmp.h> is
renamed to <bits/ranges_cmp.h>.

libstdc++-v3/ChangeLog:

	* include/Makefile.am: Add new headers and adjust for renamed
	header.
	* include/Makefile.in: Regenerate.
	* include/bits/iterator_concepts.h: Adjust for renamed header.
	* include/bits/range_access.h (ranges::*): Move to new
	<bits/ranges_base.h> header.
	* include/bits/ranges_algobase.h: Include new <bits/ranges_base.h>
	header instead of <ranges>.
	* include/bits/ranges_algo.h: Include new <bits/ranges_util.h>
	header.
	* include/bits/range_cmp.h: Moved to...
	* include/bits/ranges_cmp.h: ...here.
	* include/bits/ranges_base.h: New header.
	* include/bits/ranges_util.h: New header.
	* include/experimental/string_view: Include new
	<bits/ranges_base.h> header.
	* include/std/functional: Adjust for renamed header.
	* include/std/ranges (ranges::view_base, ranges::enable_view)
	(ranges::dangling, ranges::borrowed_iterator_t): Move to new
	<bits/ranges_base.h> header.
	(ranges::view_interface, ranges::subrange)
	(ranges::borrowed_subrange_t): Move to new <bits/ranges_util.h>
	header.
	* include/std/span: Include new <bits/ranges_base.h> header.
	* include/std/string_view: Likewise.
	* testsuite/24_iterators/back_insert_iterator/pr93884.cc: Add
	missing <ranges> header.
	* testsuite/24_iterators/front_insert_iterator/pr93884.cc:
	Likewise.
2020-09-22 15:45:54 +01:00

196 lines
6.4 KiB
C++

// Concept-constrained comparison implementations -*- C++ -*-
// Copyright (C) 2019-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file bits/ranges_cmp.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{functional}
*/
#ifndef _RANGES_CMP_H
#define _RANGES_CMP_H 1
#if __cplusplus > 201703L
# include <bits/move.h>
# include <concepts>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __is_transparent; // not defined
// Define std::identity here so that <iterator> and <ranges>
// don't need to include <bits/stl_function.h> to get it.
/// [func.identity] The identity function.
struct identity
{
template<typename _Tp>
constexpr _Tp&&
operator()(_Tp&& __t) const noexcept
{ return std::forward<_Tp>(__t); }
using is_transparent = __is_transparent;
};
#ifdef __cpp_lib_concepts
// Define this here, included by all the headers that need to define it.
#define __cpp_lib_ranges 201911L
namespace ranges
{
namespace __detail
{
// BUILTIN-PTR-CMP(T, ==, U)
template<typename _Tp, typename _Up>
concept __eq_builtin_ptr_cmp
= requires (_Tp&& __t, _Up&& __u) { { __t == __u } -> same_as<bool>; }
&& convertible_to<_Tp, const volatile void*>
&& convertible_to<_Up, const volatile void*>
&& (! requires(_Tp&& __t, _Up&& __u)
{ operator==(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
&&
! requires(_Tp&& __t, _Up&& __u)
{ std::forward<_Tp>(__t).operator==(std::forward<_Up>(__u)); });
// BUILTIN-PTR-CMP(T, <, U)
template<typename _Tp, typename _Up>
concept __less_builtin_ptr_cmp
= requires (_Tp&& __t, _Up&& __u) { { __t < __u } -> same_as<bool>; }
&& convertible_to<_Tp, const volatile void*>
&& convertible_to<_Up, const volatile void*>
&& (! requires(_Tp&& __t, _Up&& __u)
{ operator<(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
&& ! requires(_Tp&& __t, _Up&& __u)
{ std::forward<_Tp>(__t).operator<(std::forward<_Up>(__u)); });
} // namespace __detail
// [range.cmp] Concept-constrained comparisons
/// ranges::equal_to function object type.
struct equal_to
{
template<typename _Tp, typename _Up>
requires equality_comparable_with<_Tp, _Up>
|| __detail::__eq_builtin_ptr_cmp<_Tp, _Up>
constexpr bool
operator()(_Tp&& __t, _Up&& __u) const
noexcept(noexcept(std::declval<_Tp>() == std::declval<_Up>()))
{ return std::forward<_Tp>(__t) == std::forward<_Up>(__u); }
using is_transparent = __is_transparent;
};
/// ranges::not_equal_to function object type.
struct not_equal_to
{
template<typename _Tp, typename _Up>
requires equality_comparable_with<_Tp, _Up>
|| __detail::__eq_builtin_ptr_cmp<_Tp, _Up>
constexpr bool
operator()(_Tp&& __t, _Up&& __u) const
noexcept(noexcept(std::declval<_Up>() == std::declval<_Tp>()))
{ return !equal_to{}(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
using is_transparent = __is_transparent;
};
/// ranges::less function object type.
struct less
{
template<typename _Tp, typename _Up>
requires totally_ordered_with<_Tp, _Up>
|| __detail::__less_builtin_ptr_cmp<_Tp, _Up>
constexpr bool
operator()(_Tp&& __t, _Up&& __u) const
noexcept(noexcept(std::declval<_Tp>() < std::declval<_Up>()))
{
if constexpr (__detail::__less_builtin_ptr_cmp<_Tp, _Up>)
{
#ifdef __cpp_lib_is_constant_evaluated
if (std::is_constant_evaluated())
return __t < __u;
#endif
auto __x = reinterpret_cast<__UINTPTR_TYPE__>(
static_cast<const volatile void*>(std::forward<_Tp>(__t)));
auto __y = reinterpret_cast<__UINTPTR_TYPE__>(
static_cast<const volatile void*>(std::forward<_Up>(__u)));
return __x < __y;
}
else
return std::forward<_Tp>(__t) < std::forward<_Up>(__u);
}
using is_transparent = __is_transparent;
};
/// ranges::greater function object type.
struct greater
{
template<typename _Tp, typename _Up>
requires totally_ordered_with<_Tp, _Up>
|| __detail::__less_builtin_ptr_cmp<_Up, _Tp>
constexpr bool
operator()(_Tp&& __t, _Up&& __u) const
noexcept(noexcept(std::declval<_Up>() < std::declval<_Tp>()))
{ return less{}(std::forward<_Up>(__u), std::forward<_Tp>(__t)); }
using is_transparent = __is_transparent;
};
/// ranges::greater_equal function object type.
struct greater_equal
{
template<typename _Tp, typename _Up>
requires totally_ordered_with<_Tp, _Up>
|| __detail::__less_builtin_ptr_cmp<_Tp, _Up>
constexpr bool
operator()(_Tp&& __t, _Up&& __u) const
noexcept(noexcept(std::declval<_Tp>() < std::declval<_Up>()))
{ return !less{}(std::forward<_Tp>(__t), std::forward<_Up>(__u)); }
using is_transparent = __is_transparent;
};
/// ranges::less_equal function object type.
struct less_equal
{
template<typename _Tp, typename _Up>
requires totally_ordered_with<_Tp, _Up>
|| __detail::__less_builtin_ptr_cmp<_Up, _Tp>
constexpr bool
operator()(_Tp&& __t, _Up&& __u) const
noexcept(noexcept(std::declval<_Up>() < std::declval<_Tp>()))
{ return !less{}(std::forward<_Up>(__u), std::forward<_Tp>(__t)); }
using is_transparent = __is_transparent;
};
} // namespace ranges
#endif // library concepts
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++20
#endif // _RANGES_CMP_H