From: Jonathan Wakely Date: Tue, 14 May 2019 15:25:01 +0000 (+0100) Subject: Define std::__invoke_r for INVOKE X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=78c2855df612af45edb78426348296f43f2a8602;p=gcc.git Define std::__invoke_r for INVOKE * include/bits/invoke.h (__invoke_r): Define new function implementing the INVOKE pseudo-function. * testsuite/20_util/function_objects/invoke/1.cc: Add more tests. * testsuite/20_util/function_objects/invoke/2.cc: New test. From-SVN: r271173 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index fced2f98538..8330ad1a308 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2019-05-14 Jonathan Wakely + * include/bits/invoke.h (__invoke_r): Define new function implementing + the INVOKE pseudo-function. + * testsuite/20_util/function_objects/invoke/1.cc: Add more tests. + * testsuite/20_util/function_objects/invoke/2.cc: New test. + * include/std/type_traits (__is_nt_convertible_helper): Define it unconditionally, not only for C++20. (__is_nothrow_convertible): Define internal trait for use in C++11. diff --git a/libstdc++-v3/include/bits/invoke.h b/libstdc++-v3/include/bits/invoke.h index a5278a59f0c..59e22da84d4 100644 --- a/libstdc++-v3/include/bits/invoke.h +++ b/libstdc++-v3/include/bits/invoke.h @@ -96,6 +96,65 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::forward<_Args>(__args)...); } +#if __cplusplus >= 201703L + // INVOKE: Invoke a callable object and convert the result to R. + template + constexpr enable_if_t, _Res> + __invoke_r(_Callable&& __fn, _Args&&... __args) + noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>) + { + using __result = __invoke_result<_Callable, _Args...>; + using __type = typename __result::type; + using __tag = typename __result::__invoke_type; + if constexpr (is_void_v<_Res>) + std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn), + std::forward<_Args>(__args)...); + else + return std::__invoke_impl<__type>(__tag{}, + std::forward<_Callable>(__fn), + std::forward<_Args>(__args)...); + } +#else // C++11 + template + using __can_invoke_as_void = __enable_if_t< + __and_, __is_invocable<_Callable, _Args...>>::value, + _Res + >; + + template + using __can_invoke_as_nonvoid = __enable_if_t< + __and_<__not_>, + is_convertible::type, + _Res> + >::value, + _Res + >; + + // INVOKE: Invoke a callable object and convert the result to R. + template + constexpr __can_invoke_as_nonvoid<_Res, _Callable, _Args...> + __invoke_r(_Callable&& __fn, _Args&&... __args) + { + using __result = __invoke_result<_Callable, _Args...>; + using __type = typename __result::type; + using __tag = typename __result::__invoke_type; + return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn), + std::forward<_Args>(__args)...); + } + + // INVOKE when R is cv void + template + constexpr __can_invoke_as_void<_Res, _Callable, _Args...> + __invoke_r(_Callable&& __fn, _Args&&... __args) + { + using __result = __invoke_result<_Callable, _Args...>; + using __type = typename __result::type; + using __tag = typename __result::__invoke_type; + std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn), + std::forward<_Args>(__args)...); + } +#endif // C++11 + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/testsuite/20_util/function_objects/invoke/1.cc b/libstdc++-v3/testsuite/20_util/function_objects/invoke/1.cc index e6a4a2ac560..9af7b294130 100644 --- a/libstdc++-v3/testsuite/20_util/function_objects/invoke/1.cc +++ b/libstdc++-v3/testsuite/20_util/function_objects/invoke/1.cc @@ -24,7 +24,43 @@ struct abstract { void operator()() noexcept; }; -static_assert( noexcept(std::__invoke(std::declval())), "" ); +static_assert( noexcept(std::__invoke(std::declval())), + "It should be possible to use abstract types with INVOKE" ); #if __cpp_lib_invoke -static_assert( noexcept(std::invoke(std::declval())), "" ); +// std::invoke is only defined since C++17. +static_assert( noexcept(std::invoke(std::declval())), + "It should be possible to use abstract types with INVOKE" ); + +// The std::__invoke_r extension only has a noexcept-specifier for >= C++17. +static_assert( noexcept(std::__invoke_r(std::declval())), + "It should be possible to use abstract types with INVOKE" ); +#endif + +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)), "" ); + +#if __cpp_lib_invoke +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)), "" ); #endif diff --git a/libstdc++-v3/testsuite/20_util/function_objects/invoke/2.cc b/libstdc++-v3/testsuite/20_util/function_objects/invoke/2.cc new file mode 100644 index 00000000000..df239876004 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function_objects/invoke/2.cc @@ -0,0 +1,44 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +constexpr int sq(int i) { return i * i; } + +template +bool chk(Val&& val, Expected&& exp) +{ + return std::is_same::value && val == exp; +} + +#define VERIFY_T(x,y) VERIFY(chk(x,y)) + +void +test01() +{ + VERIFY_T( std::__invoke(sq, 2), 4 ); + VERIFY_T( std::__invoke_r(sq, 3), 9 ); + VERIFY_T( std::__invoke_r(sq, 4), '\x10' ); +} + +int main() +{ + test01(); +}