public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-3316] libstdc++: Define std::invoke_r for C++23 (P2136R3) Date: Thu, 2 Sep 2021 17:51:11 +0000 (GMT) [thread overview] Message-ID: <20210902175111.3DC2B3858001@sourceware.org> (raw) https://gcc.gnu.org/g:5b73abd1a5f44f72e36bc7aefd423816083291ea commit r12-3316-g5b73abd1a5f44f72e36bc7aefd423816083291ea Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Sep 2 11:54:12 2021 +0100 libstdc++: Define std::invoke_r for C++23 (P2136R3) We already supported this feature as std::__invoke<R>, for internal use. This just adds a public version of it to <functional>. Internal uses should continue to include <bits/invoke.h> and use std::__invoke<R> so that they don't need to include all of <functional>. Signed-off-by: Jonathan Wakely <jwakely@redhat.com> libstdc++-v3/ChangeLog: * include/std/functional (invoke_r): Define. * include/std/version (__cpp_lib_invoke_r): Define. * testsuite/20_util/function_objects/invoke/version.cc: Check for __cpp_lib_invoke_r as well as __cpp_lib_invoke. * testsuite/20_util/function_objects/invoke/4.cc: New test. Diff: --- libstdc++-v3/include/std/functional | 15 ++++++ libstdc++-v3/include/std/version | 1 + .../testsuite/20_util/function_objects/invoke/4.cc | 59 ++++++++++++++++++++++ .../20_util/function_objects/invoke/version.cc | 10 ++++ 4 files changed, 85 insertions(+) diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index 131e6629341..0b257926fd5 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -96,6 +96,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return std::__invoke(std::forward<_Callable>(__fn), std::forward<_Args>(__args)...); } + +#if __cplusplus > 202002L +# define __cpp_lib_invoke_r 202106L + + /// Invoke a callable object and convert the result to _Res. + template<typename _Res, typename _Callable, typename... _Args> + requires is_invocable_r_v<_Res, _Callable, _Args...> + constexpr _Res + invoke_r(_Callable&& __fn, _Args&&... __args) + noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>) + { + return std::__invoke_r<_Res>(std::forward<_Callable>(__fn), + std::forward<_Args>(__args)...); + } +#endif // C++23 #endif // C++17 template<typename _MemFunPtr, diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index 70d573bb517..f950bf0f0db 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -278,6 +278,7 @@ #if __cplusplus > 202002L // c++2b +#define __cpp_lib_invoke_r 202106L #define __cpp_lib_is_scoped_enum 202011L #define __cpp_lib_string_contains 202011L #define __cpp_lib_to_underlying 202102L diff --git a/libstdc++-v3/testsuite/20_util/function_objects/invoke/4.cc b/libstdc++-v3/testsuite/20_util/function_objects/invoke/4.cc new file mode 100644 index 00000000000..3ee6711f687 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function_objects/invoke/4.cc @@ -0,0 +1,59 @@ +// { dg-options "-std=gnu++2b" } +// { dg-do compile { target c++23 } } + +#include <functional> + +#ifndef __cpp_lib_invoke_r +# error Feature-test macro for invoke_r is missing in <functional> +#elif __cpp_lib_invoke_r < 202106L +# error Feature-test macro for invoke_r has the wrong value in <functional> +#endif + +constexpr int sq(int i) { return i * i; } + +template<typename Val, typename Expected> +constexpr bool chk(Val&& val, Expected&& exp) +{ + return std::is_same_v<Val, Expected> && val == exp; +} + +void +test01() +{ + static_assert( chk( std::invoke(sq, 2), 4 ) ); + static_assert( chk( std::invoke_r<int>(sq, 3), 9 ) ); + static_assert( chk( std::invoke_r<char>(sq, 4), '\x10' ) ); +} + +struct abstract { + virtual ~abstract() = 0; + void operator()() noexcept; +}; + +static_assert( noexcept(std::invoke(std::declval<abstract>())), + "It should be possible to use abstract types with INVOKE" ); + +static_assert( noexcept(std::invoke_r<void>(std::declval<abstract>())), + "It should be possible to use abstract types with INVOKE<R>" ); + +struct F { + void operator()() &; + void operator()() && noexcept; + int operator()(int); + double* operator()(int, int) noexcept; +}; +struct D { D(void*); }; + +static_assert( !noexcept(std::invoke(std::declval<F&>())) ); +static_assert( noexcept(std::invoke(std::declval<F>())) ); +static_assert( !noexcept(std::invoke(std::declval<F>(), 1)) ); +static_assert( noexcept(std::invoke(std::declval<F>(), 1, 2)) ); + +static_assert( !noexcept(std::invoke_r<void>(std::declval<F&>())) ); +static_assert( noexcept(std::invoke_r<void>(std::declval<F>())) ); +static_assert( !noexcept(std::invoke_r<int>(std::declval<F>(), 1)) ); +static_assert( !noexcept(std::invoke_r<void>(std::declval<F>(), 1)) ); +static_assert( !noexcept(std::invoke_r<long>(std::declval<F>(), 1)) ); +static_assert( noexcept(std::invoke_r<void>(std::declval<F>(), 1, 2)) ); +static_assert( noexcept(std::invoke_r<void*>(std::declval<F>(), 1, 2)) ); +static_assert( !noexcept(std::invoke_r<D>(std::declval<F>(), 1, 2)) ); diff --git a/libstdc++-v3/testsuite/20_util/function_objects/invoke/version.cc b/libstdc++-v3/testsuite/20_util/function_objects/invoke/version.cc index cf1a46a1ada..2dc71aea504 100644 --- a/libstdc++-v3/testsuite/20_util/function_objects/invoke/version.cc +++ b/libstdc++-v3/testsuite/20_util/function_objects/invoke/version.cc @@ -7,3 +7,13 @@ #elif __cpp_lib_invoke < 201411L # error Feature-test macro for invoke has the wrong value in <version> #endif + +#if __cplusplus > 202002L +#ifndef __cpp_lib_invoke_r +# error Feature-test macro for invoke_r is missing in <version> +#elif __cpp_lib_invoke_r < 202106L +# error Feature-test macro for invoke_r has the wrong value in <version> +#endif +#elif defined __cpp_lib_invoke_r +# error __cpp_lib_invoke_r is defined in <version> before C++23 +#endif
reply other threads:[~2021-09-02 17:51 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=20210902175111.3DC2B3858001@sourceware.org \ --to=redi@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).