diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2019-05-14 16:25:01 +0100 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2019-05-14 16:25:01 +0100 |
commit | 78c2855df612af45edb78426348296f43f2a8602 (patch) | |
tree | 9ea7828eaefb76469113fbda0311e8f8bbc5b769 /libstdc++-v3 | |
parent | 1ac09ef2c611d3113665ec8c74e38b125217edb3 (diff) | |
download | gcc-78c2855df612af45edb78426348296f43f2a8602.zip gcc-78c2855df612af45edb78426348296f43f2a8602.tar.gz gcc-78c2855df612af45edb78426348296f43f2a8602.tar.bz2 |
Define std::__invoke_r for INVOKE<R>
* include/bits/invoke.h (__invoke_r): Define new function implementing
the INVOKE<R> 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
Diffstat (limited to 'libstdc++-v3')
-rw-r--r-- | libstdc++-v3/ChangeLog | 5 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/invoke.h | 59 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/function_objects/invoke/1.cc | 40 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/function_objects/invoke/2.cc | 44 |
4 files changed, 146 insertions, 2 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index fced2f9..8330ad1 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2019-05-14 Jonathan Wakely <jwakely@redhat.com> + * include/bits/invoke.h (__invoke_r): Define new function implementing + the INVOKE<R> 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 a5278a5..59e22da 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<R>: Invoke a callable object and convert the result to R. + template<typename _Res, typename _Callable, typename... _Args> + constexpr enable_if_t<is_invocable_r_v<_Res, _Callable, _Args...>, _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<typename _Res, typename _Callable, typename... _Args> + using __can_invoke_as_void = __enable_if_t< + __and_<is_void<_Res>, __is_invocable<_Callable, _Args...>>::value, + _Res + >; + + template<typename _Res, typename _Callable, typename... _Args> + using __can_invoke_as_nonvoid = __enable_if_t< + __and_<__not_<is_void<_Res>>, + is_convertible<typename __invoke_result<_Callable, _Args...>::type, + _Res> + >::value, + _Res + >; + + // INVOKE<R>: Invoke a callable object and convert the result to R. + template<typename _Res, typename _Callable, typename... _Args> + 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<R> when R is cv void + template<typename _Res, typename _Callable, typename... _Args> + 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 e6a4a2a..9af7b29 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<abstract>())), "" ); +static_assert( noexcept(std::__invoke(std::declval<abstract>())), + "It should be possible to use abstract types with INVOKE" ); #if __cpp_lib_invoke -static_assert( noexcept(std::invoke(std::declval<abstract>())), "" ); +// std::invoke is only defined since C++17. +static_assert( noexcept(std::invoke(std::declval<abstract>())), + "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<void>(std::declval<abstract>())), + "It should be possible to use abstract types with INVOKE<R>" ); +#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<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)), "" ); + +#if __cpp_lib_invoke +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)), "" ); #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 0000000..df23987 --- /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 +// <http://www.gnu.org/licenses/>. + +// { dg-do run { target c++11 } } + +#include <functional> +#include <testsuite_hooks.h> + +constexpr int sq(int i) { return i * i; } + +template<typename Val, typename Expected> +bool chk(Val&& val, Expected&& exp) +{ + return std::is_same<Val, Expected>::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<int>(sq, 3), 9 ); + VERIFY_T( std::__invoke_r<char>(sq, 4), '\x10' ); +} + +int main() +{ + test01(); +} |