public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: Patrick Palka <ppalka@redhat.com>,
	gcc-patches@gcc.gnu.org,  libstdc++@gcc.gnu.org
Subject: Re: [PATCH] libstdc++/ranges: Use perfect forwarding in _Pipe and _Partial ctors
Date: Thu, 11 Jan 2024 11:11:54 -0500 (EST)	[thread overview]
Message-ID: <286e5a28-cd2c-ea40-e4cc-0180798f18b0@idea> (raw)
In-Reply-To: <CACb0b4m6097V-rMX004gtv4HKxLXRs5XEefJQ1954z=L74UaXw@mail.gmail.com>

On Thu, 11 Jan 2024, Jonathan Wakely wrote:

> On Wed, 10 Jan 2024 at 21:40, Patrick Palka <ppalka@redhat.com> wrote:
> >
> > Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
> >
> > -- >8 --
> >
> > This avoids redundant moves when composing and partially applying range
> > adaptor objects.
> >
> > Note that the new constraints on _Partial's constructor templates are
> > needed so that it's not inadvertently chosen over the copy constructor
> > when constructing a _Partial object from a non-const _Partial lvalue.
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/std/ranges (views::__adaptor::operator|): Perform
> >         perfect forwarding of arguments.
> >         (views::__adaptor::_Partial::_Partial): Likewise.
> >         (views::__adaptor::_Pipe::__Pipe): Likewise.
> > ---
> >  libstdc++-v3/include/std/ranges | 65 ++++++++++++++++++++-------------
> >  1 file changed, 39 insertions(+), 26 deletions(-)
> >
> > diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> > index 81a857502e3..0734daa42bf 100644
> > --- a/libstdc++-v3/include/std/ranges
> > +++ b/libstdc++-v3/include/std/ranges
> > @@ -957,8 +957,11 @@ namespace views::__adaptor
> >      requires __is_range_adaptor_closure<_Lhs>
> >        && __is_range_adaptor_closure<_Rhs>
> >      constexpr auto
> > -    operator|(_Lhs __lhs, _Rhs __rhs)
> > -    { return _Pipe<_Lhs, _Rhs>{std::move(__lhs), std::move(__rhs)}; }
> > +    operator|(_Lhs&& __lhs, _Rhs&& __rhs)
> > +    {
> > +      return _Pipe<decay_t<_Lhs>, decay_t<_Rhs>>{std::forward<_Lhs>(__lhs),
> > +                                                std::forward<_Rhs>(__rhs)};
> > +    }
> >
> >    // The base class of every range adaptor non-closure.
> >    //
> > @@ -1004,10 +1007,12 @@ namespace views::__adaptor
> >      {
> >        tuple<_Args...> _M_args;
> >
> > -      constexpr
> > -      _Partial(_Args... __args)
> > -       : _M_args(std::move(__args)...)
> > -      { }
> > +      template<typename... _Ts>
> > +       requires (sizeof...(_Ts) == sizeof...(_Args))
> 
> Do we also need a !same_as constraint here? If sizeof...(_Args) == 1
> then this could be chosen instead of a constructor, no?

For the == 1 case we would have used a different partial specialization
of _Partial (one that doesn't use heavyweight std::tuple).  So checking
sizeof...(_Ts) == sizof...(_Args) instead is sufficient and simpler.
Checking sizeof...(_Args) > 1 would be sufficient as well.

> 
> Or is
> > +       constexpr
> > +       _Partial(_Ts&&... __args)
> > +         : _M_args(std::forward<_Ts>(__args)...)
> > +       { }
> >
> >        // Invoke _Adaptor with arguments __r, _M_args... according to the
> >        // value category of this _Partial object.
> > @@ -1046,10 +1051,12 @@ namespace views::__adaptor
> >      {
> >        _Arg _M_arg;
> >
> > -      constexpr
> > -      _Partial(_Arg __arg)
> > -       : _M_arg(std::move(__arg))
> > -      { }
> > +      template<typename _Tp>
> > +       requires (!same_as<remove_cvref_t<_Tp>, _Partial>)
> > +       constexpr
> > +       _Partial(_Tp&& __arg)
> > +         : _M_arg(std::forward<_Tp>(__arg))
> > +       { }
> >
> >        template<typename _Range>
> >         requires __adaptor_invocable<_Adaptor, _Range, const _Arg&>
> > @@ -1079,10 +1086,12 @@ namespace views::__adaptor
> >      {
> >        tuple<_Args...> _M_args;
> >
> > -      constexpr
> > -      _Partial(_Args... __args)
> > -       : _M_args(std::move(__args)...)
> > -      { }
> > +      template<typename... _Ts>
> > +       requires (sizeof...(_Ts) == sizeof...(_Args))
> > +       constexpr
> > +       _Partial(_Ts&&... __args)
> > +         : _M_args(std::forward<_Ts>(__args)...)
> > +       { }
> >
> >        // Invoke _Adaptor with arguments __r, const _M_args&... regardless
> >        // of the value category of this _Partial object.
> > @@ -1109,10 +1118,12 @@ namespace views::__adaptor
> >      {
> >        _Arg _M_arg;
> >
> > -      constexpr
> > -      _Partial(_Arg __arg)
> > -       : _M_arg(std::move(__arg))
> > -      { }
> > +      template<typename _Tp>
> > +       requires (!same_as<remove_cvref_t<_Tp>, _Partial>)
> > +       constexpr
> > +       _Partial(_Tp&& __arg)
> > +         : _M_arg(std::forward<_Tp>(__arg))
> > +       { }
> >
> >        template<typename _Range>
> >         requires __adaptor_invocable<_Adaptor, _Range, const _Arg&>
> > @@ -1135,10 +1146,11 @@ namespace views::__adaptor
> >        [[no_unique_address]] _Lhs _M_lhs;
> >        [[no_unique_address]] _Rhs _M_rhs;
> >
> > -      constexpr
> > -      _Pipe(_Lhs __lhs, _Rhs __rhs)
> > -       : _M_lhs(std::move(__lhs)), _M_rhs(std::move(__rhs))
> > -      { }
> > +      template<typename _Tp, typename _Up>
> > +       constexpr
> > +       _Pipe(_Tp&& __lhs, _Up&& __rhs)
> > +         : _M_lhs(std::forward<_Tp>(__lhs)), _M_rhs(std::forward<_Up>(__rhs))
> > +       { }
> >
> >        // Invoke _M_rhs(_M_lhs(__r)) according to the value category of this
> >        // range adaptor closure object.
> > @@ -1172,10 +1184,11 @@ namespace views::__adaptor
> >        [[no_unique_address]] _Lhs _M_lhs;
> >        [[no_unique_address]] _Rhs _M_rhs;
> >
> > -      constexpr
> > -      _Pipe(_Lhs __lhs, _Rhs __rhs)
> > -       : _M_lhs(std::move(__lhs)), _M_rhs(std::move(__rhs))
> > -      { }
> > +      template<typename _Tp, typename _Up>
> > +       constexpr
> > +       _Pipe(_Tp&& __lhs, _Up&& __rhs)
> > +         : _M_lhs(std::forward<_Tp>(__lhs)), _M_rhs(std::forward<_Up>(__rhs))
> > +       { }
> >
> >        template<typename _Range>
> >         requires __pipe_invocable<const _Lhs&, const _Rhs&, _Range>
> > --
> > 2.43.0.283.ga54a84b333
> >
> 
> 


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

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10 21:40 Patrick Palka
2024-01-11 15:28 ` Jonathan Wakely
2024-01-11 16:11   ` Patrick Palka [this message]
2024-01-11 22:56     ` Jonathan Wakely

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=286e5a28-cd2c-ea40-e4cc-0180798f18b0@idea \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    /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).