public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org
Subject: Re: [PATCH] libstdc++/ranges: Use C++23 deducing this for _Pipe and _Partial
Date: Thu, 11 Jan 2024 15:30:15 +0000	[thread overview]
Message-ID: <CACb0b4kN3Tgu-dV=ov7HpJn7MCJKLZ2GQAF_snyuEZzrtnhD5g@mail.gmail.com> (raw)
In-Reply-To: <20240111002623.2514687-1-ppalka@redhat.com>

On Thu, 11 Jan 2024 at 00:26, Patrick Palka <ppalka@redhat.com> wrote:
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?

OK. Nice to see explicit object functions landing in the library so soon!


>
> -- >8 --
>
> 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&&.  This shouldn't make a difference in practice, I think..
>
> libstdc++-v3/ChangeLog:
>
>         * include/bits/move.h (__like_t): Define.
>         * 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 "no match for call" errors issued in C++23 mode instead
>         of "use of deleted function".
>         * testsuite/std/ranges/adaptors/lazy_split_neg.cc: Likewise.
> ---
>  libstdc++-v3/include/bits/move.h              |  3 ++
>  libstdc++-v3/include/std/ranges               | 37 ++++++++++++++++++-
>  .../testsuite/std/ranges/adaptors/100577.cc   | 18 ++++-----
>  .../std/ranges/adaptors/lazy_split_neg.cc     |  2 +-
>  4 files changed, 48 insertions(+), 12 deletions(-)
>
> diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h
> index 4e741bcdeb0..bb200c95964 100644
> --- a/libstdc++-v3/include/bits/move.h
> +++ b/libstdc++-v3/include/bits/move.h
> @@ -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
>
>    /**
> diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> index 0734daa42bf..8d2649cf5b9 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -1016,7 +1016,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
> @@ -1042,6 +1054,7 @@ namespace views::__adaptor
>        template<typename _Range>
>         constexpr auto
>         operator()(_Range&& __r) const && = delete;
> +#endif
>      };
>
>    // A lightweight specialization of the above primary template for
> @@ -1058,6 +1071,14 @@ namespace views::__adaptor
>           : _M_arg(std::forward<_Tp>(__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
> @@ -1073,6 +1094,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
> @@ -1154,7 +1176,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
> @@ -1170,6 +1202,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
> diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/100577.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/100577.cc
> index 69072d69fec..3a52f5b4ce0 100644
> --- a/libstdc++-v3/testsuite/std/ranges/adaptors/100577.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/100577.cc
> @@ -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
> diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split_neg.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split_neg.cc
> index 683ea76da07..85632759fcc 100644
> --- a/libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split_neg.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split_neg.cc
> @@ -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" }
>  }
>
> --
> 2.43.0.283.ga54a84b333
>


      reply	other threads:[~2024-01-11 15:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  0:26 Patrick Palka
2024-01-11 15:30 ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACb0b4kN3Tgu-dV=ov7HpJn7MCJKLZ2GQAF_snyuEZzrtnhD5g@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).