libstdc++/ranges: Use C++23 deducing this in _Pipe and _Partial

This simplifies the operator() of the _Pipe and _Partial range adaptor
closure objects using C++23 deducing this, allowing us to condense
multiple operator() overloads into one.

The new __like_t alias template is similar to the expositional one from
P0847R6 except it's implemented in terms of forward_like instead of vice
versa, and thus ours always yields a reference so e.g. __like_t<A, char>
is char&& instead of char.  For our purposes (forwarding) this shouldn't
make a difference, I think..

libstdc++-v3/ChangeLog:

	* include/bits/move.h (__like_t): Define in C++23 mode.
	* include/std/ranges (views::__adaptor::Partial::operator()):
	Implement using C++23 deducing this when available.
	(views::__adaptor::_Pipe::operator()): Likewise.
	* testsuite/std/ranges/adaptors/100577.cc: Adjust testcase to
	accept new "no match for call" errors issued in C++23 mode.
	* testsuite/std/ranges/adaptors/lazy_split_neg.cc: Likewise.
This commit is contained in:
Patrick Palka
2024-01-11 13:14:46 -05:00
parent a2be4e1559
commit d2cb4693a0
4 changed files with 47 additions and 12 deletions

View File

@@ -110,6 +110,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return static_cast<_Up&>(__x);
}
}
template<typename _Tp, typename _Up>
using __like_t = decltype(std::forward_like<_Tp>(std::declval<_Up>()));
#endif
/**

View File

@@ -1011,7 +1011,19 @@ namespace views::__adaptor
// Invoke _Adaptor with arguments __r, _M_args... according to the
// value category of this _Partial object.
// TODO: use explicit object functions ("deducing this").
#if __cpp_explicit_this_parameter
template<typename _Self, typename _Range>
requires __adaptor_invocable<_Adaptor, _Range, __like_t<_Self, _Args>...>
constexpr auto
operator()(this _Self&& __self, _Range&& __r)
{
auto __forwarder = [&__r] (auto&&... __args) {
return _Adaptor{}(std::forward<_Range>(__r),
std::forward<decltype(__args)>(__args)...);
};
return std::apply(__forwarder, std::forward<_Self>(__self)._M_args);
}
#else
template<typename _Range>
requires __adaptor_invocable<_Adaptor, _Range, const _Args&...>
constexpr auto
@@ -1037,6 +1049,7 @@ namespace views::__adaptor
template<typename _Range>
constexpr auto
operator()(_Range&& __r) const && = delete;
#endif
};
// A lightweight specialization of the above primary template for
@@ -1051,6 +1064,13 @@ namespace views::__adaptor
: _M_arg(std::move(__arg))
{ }
#if __cpp_explicit_this_parameter
template<typename _Self, typename _Range>
requires __adaptor_invocable<_Adaptor, _Range, __like_t<_Self, _Arg>>
constexpr auto
operator()(this _Self&& __self, _Range&& __r)
{ return _Adaptor{}(std::forward<_Range>(__r), std::forward<_Self>(__self)._M_arg); }
#else
template<typename _Range>
requires __adaptor_invocable<_Adaptor, _Range, const _Arg&>
constexpr auto
@@ -1066,6 +1086,7 @@ namespace views::__adaptor
template<typename _Range>
constexpr auto
operator()(_Range&& __r) const && = delete;
#endif
};
// Partial specialization of the primary template for the case where the extra
@@ -1142,7 +1163,17 @@ namespace views::__adaptor
// Invoke _M_rhs(_M_lhs(__r)) according to the value category of this
// range adaptor closure object.
// TODO: use explicit object functions ("deducing this").
#if __cpp_explicit_this_parameter
template<typename _Self, typename _Range>
requires __pipe_invocable<__like_t<_Self, _Lhs>, __like_t<_Self, _Rhs>, _Range>
constexpr auto
operator()(this _Self&& __self, _Range&& __r)
{
return (std::forward<_Self>(__self)._M_rhs
(std::forward<_Self>(__self)._M_lhs
(std::forward<_Range>(__r))));
}
#else
template<typename _Range>
requires __pipe_invocable<const _Lhs&, const _Rhs&, _Range>
constexpr auto
@@ -1158,6 +1189,7 @@ namespace views::__adaptor
template<typename _Range>
constexpr auto
operator()(_Range&& __r) const && = delete;
#endif
};
// A partial specialization of the above primary template for the case where

View File

@@ -98,28 +98,28 @@ test02()
(views::take_while(badarg) | views::all)(x); // { dg-error "no match" }
(views::drop_while(badarg) | views::all)(x); // { dg-error "no match" }
// In practice, range adaptor closures with non-simple operator() are
// In C++20 mode, range adaptor closures with non-simple operator() are
// implemented using a fallback deleted overload, so when a call is
// ill-formed overload resolution succeeds but selects the deleted overload
// (but only when the closure is invoked as an rvalue).
views::lazy_split(badarg)(x); // { dg-error "deleted function" }
(views::lazy_split(badarg) | views::all)(x); // { dg-error "deleted function" }
views::lazy_split(badarg)(x); // { dg-error "deleted function|no match" }
(views::lazy_split(badarg) | views::all)(x); // { dg-error "deleted function|no match" }
auto a0 = views::lazy_split(badarg);
a0(x); // { dg-error "no match" };
auto a1 = a0 | views::all;
a1(x); // { dg-error "no match" }
views::split(badarg)(x); // { dg-error "deleted function" }
(views::split(badarg) | views::all)(x); // { dg-error "deleted function" }
views::split(badarg)(x); // { dg-error "deleted function|no match" }
(views::split(badarg) | views::all)(x); // { dg-error "deleted function|no match" }
auto a0a = views::split(badarg);
a0a(x); // { dg-error "no match" };
auto a1a = a0a | views::all;
a1a(x); // { dg-error "no match" }
views::take(badarg)(x); // { dg-error "deleted" }
views::drop(badarg)(x); // { dg-error "deleted" }
(views::take(badarg) | views::all)(x); // { dg-error "deleted" }
(views::drop(badarg) | views::all)(x); // { dg-error "deleted" }
views::take(badarg)(x); // { dg-error "deleted|no match" }
views::drop(badarg)(x); // { dg-error "deleted|no match" }
(views::take(badarg) | views::all)(x); // { dg-error "deleted|no match" }
(views::drop(badarg) | views::all)(x); // { dg-error "deleted|no match" }
}
void

View File

@@ -38,7 +38,7 @@ test02()
{
using namespace std::literals;
auto x = "the quick brown fox"sv;
auto v1 = views::lazy_split(std::initializer_list<char>{' ', ' '})(x); // { dg-error "deleted" }
auto v1 = views::lazy_split(std::initializer_list<char>{' ', ' '})(x); // { dg-error "deleted|no match" }
auto v2 = x | views::lazy_split(std::initializer_list<char>{' ', ' '}); // { dg-error "no match" }
}