* 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.

Tested powerpc64le-linux, committed to trunk.

commit 2a1977fbb243b6551de42a434a316fb033580f79
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Tue Apr 9 20:09:00 2019 +0100

    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.

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

Reply via email to