public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Filip Kastl <pheeck@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/users/pheeck/heads/sccp)] libstdc++: Reduce size of std::bind_front(F) result Date: Wed, 15 Feb 2023 10:17:38 +0000 (GMT) [thread overview] Message-ID: <20230215101738.D9AF03858004@sourceware.org> (raw) https://gcc.gnu.org/g:886985fc320f2bad3a902ffbcad7faecf28110e7 commit 886985fc320f2bad3a902ffbcad7faecf28110e7 Author: Jonathan Wakely <jwakely@redhat.com> 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<typename _Fd> + 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<typename _Fn> + 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<typename... _CallArgs> + 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<typename... _CallArgs> + constexpr + invoke_result_t<const _Fd&, _CallArgs...> + operator()(_CallArgs&&... __call_args) const & + noexcept(is_nothrow_invocable_v<const _Fd&, _CallArgs...>) + { return std::invoke(_M_fd, std::forward<_CallArgs>(__call_args)...); } + + template<typename... _CallArgs> + 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<typename... _CallArgs> + constexpr + invoke_result_t<const _Fd, _CallArgs...> + operator()(_CallArgs&&... __call_args) const && + noexcept(is_nothrow_invocable_v<const _Fd, _CallArgs...>) + { + return std::invoke(std::move(_M_fd), + std::forward<_CallArgs>(__call_args)...); + } + + private: + _Fd _M_fd; + }; + template<typename _Fn, typename... _Args> using _Bind_front_t - = _Bind_front<decay_t<_Fn>, decay_t<_Args>...>; + = __conditional_t<sizeof...(_Args) == 0, _Bind_front0<decay_t<_Fn>>, + _Bind_front<decay_t<_Fn>, 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 <functional> + +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) );
reply other threads:[~2023-02-15 10:17 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20230215101738.D9AF03858004@sourceware.org \ --to=pheeck@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@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: linkBe 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).