From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 937CB384F036; Mon, 21 Nov 2022 18:51:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 937CB384F036 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669056671; bh=7ptuOPQPeKytBdFbp6P/P+h0nK9nLkGSDU1sC73nZG4=; h=From:To:Subject:Date:From; b=h92mu5Wnonw8Xf4GUNJQpID3P/ETvD5jMq4DpGzmro1UlbtaZiMOh2AsdrTwsyTY7 GR/sjDguoeJuXymkGCYqdoEcOA1tzsE28yh4Rxf4zreJfLVdIUWHUP/4L89TNGkCmG smKrhNrSaPFs6BaEV7BNGk3BmCawIhsNC+6iE/AQ= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-4214] libstdc++: Reduce size of std::bind_front(F) result X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: ed77dcb9be76e592b62449c75a5e751485514afd X-Git-Newrev: cbd05ca5ab1231d70d07e1ffbe64c1b7338bf1d6 Message-Id: <20221121185111.937CB384F036@sourceware.org> Date: Mon, 21 Nov 2022 18:51:11 +0000 (GMT) List-Id: https://gcc.gnu.org/g:cbd05ca5ab1231d70d07e1ffbe64c1b7338bf1d6 commit r13-4214-gcbd05ca5ab1231d70d07e1ffbe64c1b7338bf1d6 Author: Jonathan Wakely Date: Mon Nov 21 11:30:55 2022 +0000 libstdc++: Reduce size of std::bind_front(F) result When there are no bound arguments to a std::bind_front call we don't need the overhead of compiling, initializing, and accessing an empty tuple. libstdc++-v3/ChangeLog: * include/std/functional (_Bind_front0): New class template. (_Bind_front_t): Use _Bind_front0 when there are no bound arguments. * testsuite/20_util/function_objects/bind_front/107784.cc: New test. Diff: --- libstdc++-v3/include/std/functional | 62 +++++++++++++++++++++- .../20_util/function_objects/bind_front/107784.cc | 15 ++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index b396e8dbbdc..dddd22fcd04 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -995,9 +995,69 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::tuple<_BoundArgs...> _M_bound_args; }; + // Avoid the overhead of an empty tuple<> if there are no bound args. + template + struct _Bind_front0 + { + static_assert(is_move_constructible_v<_Fd>); + + // First parameter is to ensure this constructor is never used + // instead of the copy/move constructor. + template + explicit constexpr + _Bind_front0(int, _Fn&& __fn) + noexcept(is_nothrow_constructible_v<_Fd, _Fn>) + : _M_fd(std::forward<_Fn>(__fn)) + { } + + _Bind_front0(const _Bind_front0&) = default; + _Bind_front0(_Bind_front0&&) = default; + _Bind_front0& operator=(const _Bind_front0&) = default; + _Bind_front0& operator=(_Bind_front0&&) = default; + ~_Bind_front0() = default; + + template + constexpr + invoke_result_t<_Fd&, _CallArgs...> + operator()(_CallArgs&&... __call_args) & + noexcept(is_nothrow_invocable_v<_Fd&, _CallArgs...>) + { return std::invoke(_M_fd, std::forward<_CallArgs>(__call_args)...); } + + template + constexpr + invoke_result_t + operator()(_CallArgs&&... __call_args) const & + noexcept(is_nothrow_invocable_v) + { return std::invoke(_M_fd, std::forward<_CallArgs>(__call_args)...); } + + template + constexpr + invoke_result_t<_Fd, _CallArgs...> + operator()(_CallArgs&&... __call_args) && + noexcept(is_nothrow_invocable_v<_Fd, _CallArgs...>) + { + return std::invoke(std::move(_M_fd), + std::forward<_CallArgs>(__call_args)...); + } + + template + constexpr + invoke_result_t + operator()(_CallArgs&&... __call_args) const && + noexcept(is_nothrow_invocable_v) + { + return std::invoke(std::move(_M_fd), + std::forward<_CallArgs>(__call_args)...); + } + + private: + _Fd _M_fd; + }; + template using _Bind_front_t - = _Bind_front, decay_t<_Args>...>; + = __conditional_t>, + _Bind_front, decay_t<_Args>...>>; /** Create call wrapper by partial application of arguments to function. * diff --git a/libstdc++-v3/testsuite/20_util/function_objects/bind_front/107784.cc b/libstdc++-v3/testsuite/20_util/function_objects/bind_front/107784.cc new file mode 100644 index 00000000000..ec255f3ee36 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function_objects/bind_front/107784.cc @@ -0,0 +1,15 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } + +#include + +struct Foo +{ + void func() {} +}; + +void bar() { } + +// PR libstdc++/107784 +static_assert( sizeof(std::bind_front(&Foo::func)) == sizeof(&Foo::func) ); +static_assert( sizeof(std::bind_front(&bar)) == sizeof(&bar) );