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.
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
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
--- /dev/null
+// 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();
+}