From: Patrick Palka Date: Sat, 13 Jan 2024 03:54:59 +0000 (-0500) Subject: libstdc++/ranges: Use perfect forwarding in _Pipe and _Partial ctors X-Git-Tag: basepoints/gcc-15~2921 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c48bedd180672276cc58f379a6346309366b7ea7;p=thirdparty%2Fgcc.git libstdc++/ranges: Use perfect forwarding in _Pipe and _Partial ctors This avoids redundant moves when composing and partially applying range adaptor objects. libstdc++-v3/ChangeLog: * include/std/ranges (views::__adaptor::operator|): Perform perfect forwarding of arguments. (views::__adaptor::_RangeAdaptor::operator()): Pass dummy first argument to _Partial. (views::__adaptor::_Partial::_Partial): Likewise. Add dummy first parameter. (views::__adaptor::_Pipe::_Pipe): Perform perfect forwarding of arguments. (to): Pass dummy first argument to _Partial. --- diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 5ef4443fc646..3135e6f0c080 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -956,8 +956,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<_Rhs>>{std::forward<_Lhs>(__lhs), + std::forward<_Rhs>(__rhs)}; + } // The base class of every range adaptor non-closure. // @@ -980,7 +983,7 @@ namespace views::__adaptor constexpr auto operator()(_Args&&... __args) const { - return _Partial<_Derived, decay_t<_Args>...>{std::forward<_Args>(__args)...}; + return _Partial<_Derived, decay_t<_Args>...>{0, std::forward<_Args>(__args)...}; } }; @@ -1003,10 +1006,13 @@ namespace views::__adaptor { tuple<_Args...> _M_args; - constexpr - _Partial(_Args... __args) - : _M_args(std::move(__args)...) - { } + // First parameter is to ensure this constructor is never used + // instead of the copy/move constructor. + template + constexpr + _Partial(int, _Ts&&... __args) + : _M_args(std::forward<_Ts>(__args)...) + { } // Invoke _Adaptor with arguments __r, _M_args... according to the // value category of this _Partial object. @@ -1058,10 +1064,11 @@ namespace views::__adaptor { _Arg _M_arg; - constexpr - _Partial(_Arg __arg) - : _M_arg(std::move(__arg)) - { } + template + constexpr + _Partial(int, _Tp&& __arg) + : _M_arg(std::forward<_Tp>(__arg)) + { } #if __cpp_explicit_this_parameter template @@ -1099,10 +1106,11 @@ namespace views::__adaptor { tuple<_Args...> _M_args; - constexpr - _Partial(_Args... __args) - : _M_args(std::move(__args)...) - { } + template + constexpr + _Partial(int, _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. @@ -1129,10 +1137,11 @@ namespace views::__adaptor { _Arg _M_arg; - constexpr - _Partial(_Arg __arg) - : _M_arg(std::move(__arg)) - { } + template + constexpr + _Partial(int, _Tp&& __arg) + : _M_arg(std::forward<_Tp>(__arg)) + { } template requires __adaptor_invocable<_Adaptor, _Range, const _Arg&> @@ -1155,10 +1164,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 + 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. @@ -1203,10 +1213,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 + constexpr + _Pipe(_Tp&& __lhs, _Up&& __rhs) + : _M_lhs(std::forward<_Tp>(__lhs)), _M_rhs(std::forward<_Up>(__rhs)) + { } template requires __pipe_invocable @@ -9458,7 +9469,7 @@ namespace __detail { using __detail::_To; using views::__adaptor::_Partial; - return _Partial<_To<_Cont>, decay_t<_Args>...>{std::forward<_Args>(__args)...}; + return _Partial<_To<_Cont>, decay_t<_Args>...>{0, std::forward<_Args>(__args)...}; } /// @cond undocumented @@ -9503,7 +9514,7 @@ namespace __detail { using __detail::_To2; using views::__adaptor::_Partial; - return _Partial<_To2<_Cont>, decay_t<_Args>...>{std::forward<_Args>(__args)...}; + return _Partial<_To2<_Cont>, decay_t<_Args>...>{0, std::forward<_Args>(__args)...}; } } // namespace ranges