From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 303A53857C50 for ; Thu, 2 Sep 2021 17:52:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 303A53857C50 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-143-tghGeQYFPsGtoazALEHaLA-1; Thu, 02 Sep 2021 13:52:25 -0400 X-MC-Unique: tghGeQYFPsGtoazALEHaLA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 56303107ACC7; Thu, 2 Sep 2021 17:52:24 +0000 (UTC) Received: from localhost (unknown [10.33.36.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id E13D42854D; Thu, 2 Sep 2021 17:52:23 +0000 (UTC) Date: Thu, 2 Sep 2021 18:52:23 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Define std::invoke_r for C++23 (P2136R3) Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="prB9GL5uZ7sFG4fj" Content-Disposition: inline X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Sep 2021 17:52:37 -0000 --prB9GL5uZ7sFG4fj Content-Type: text/plain; charset=us-ascii Content-Disposition: inline We already supported this feature as std::__invoke, for internal use. This just adds a public version of it to . Internal uses should continue to include and use std::__invoke so that they don't need to include all of . Signed-off-by: Jonathan Wakely 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. Tested x86_64-linux. Committed to trunk. --prB9GL5uZ7sFG4fj Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 5b73abd1a5f44f72e36bc7aefd423816083291ea Author: Jonathan Wakely Date: Thu Sep 2 11:54:12 2021 libstdc++: Define std::invoke_r for C++23 (P2136R3) We already supported this feature as std::__invoke, for internal use. This just adds a public version of it to . Internal uses should continue to include and use std::__invoke so that they don't need to include all of . Signed-off-by: Jonathan Wakely 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 --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 + 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 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 + +#ifndef __cpp_lib_invoke_r +# error Feature-test macro for invoke_r is missing in +#elif __cpp_lib_invoke_r < 202106L +# error Feature-test macro for invoke_r has the wrong value in +#endif + +constexpr int sq(int i) { return i * i; } + +template +constexpr bool chk(Val&& val, Expected&& exp) +{ + return std::is_same_v && val == exp; +} + +void +test01() +{ + static_assert( chk( std::invoke(sq, 2), 4 ) ); + static_assert( chk( std::invoke_r(sq, 3), 9 ) ); + static_assert( chk( std::invoke_r(sq, 4), '\x10' ) ); +} + +struct abstract { + virtual ~abstract() = 0; + void operator()() noexcept; +}; + +static_assert( noexcept(std::invoke(std::declval())), + "It should be possible to use abstract types with INVOKE" ); + +static_assert( noexcept(std::invoke_r(std::declval())), + "It should be possible to use abstract types with INVOKE" ); + +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())) ); +static_assert( noexcept(std::invoke(std::declval())) ); +static_assert( !noexcept(std::invoke(std::declval(), 1)) ); +static_assert( noexcept(std::invoke(std::declval(), 1, 2)) ); + +static_assert( !noexcept(std::invoke_r(std::declval())) ); +static_assert( noexcept(std::invoke_r(std::declval())) ); +static_assert( !noexcept(std::invoke_r(std::declval(), 1)) ); +static_assert( !noexcept(std::invoke_r(std::declval(), 1)) ); +static_assert( !noexcept(std::invoke_r(std::declval(), 1)) ); +static_assert( noexcept(std::invoke_r(std::declval(), 1, 2)) ); +static_assert( noexcept(std::invoke_r(std::declval(), 1, 2)) ); +static_assert( !noexcept(std::invoke_r(std::declval(), 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 #endif + +#if __cplusplus > 202002L +#ifndef __cpp_lib_invoke_r +# error Feature-test macro for invoke_r is missing in +#elif __cpp_lib_invoke_r < 202106L +# error Feature-test macro for invoke_r has the wrong value in +#endif +#elif defined __cpp_lib_invoke_r +# error __cpp_lib_invoke_r is defined in before C++23 +#endif --prB9GL5uZ7sFG4fj--