Define std::__invoke_r for INVOKE<R>

Message ID 20190514152503.GA9003@redhat.com
State New
Headers show
Series
  • Define std::__invoke_r for INVOKE<R>
Related show

Commit Message

Jonathan Wakely May 14, 2019, 3:25 p.m.
* 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 <jwakely@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.

Comments

Stephan Bergmann May 17, 2019, 8:49 a.m. | #1
On 14/05/2019 17:25, Jonathan Wakely wrote:
>      * 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.


> 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)


I think this is a problem with -std=c++11 (but not -std=c++14) where 
void is not yet a literal type, so this function can't be constexpr?

(I came across this with Clang -std=c++11 upon #include <memory> 
producing an odd

> In file included from /home/sbergman/gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/10.0.0/../../../../include/c++/10.0.0/memory:81:

> In file included from /home/sbergman/gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/10.0.0/../../../../include/c++/10.0.0/bits/unique_ptr.h:37:

> In file included from /home/sbergman/gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/10.0.0/../../../../include/c++/10.0.0/tuple:41:

> /home/sbergman/gcc/trunk/inst/lib/gcc/x86_64-pc-linux-gnu/10.0.0/../../../../include/c++/10.0.0/bits/invoke.h:148:5: error: no return statement in constexpr function

>     __invoke_r(_Callable&& __fn, _Args&&... __args)

>     ^


I first reduced that to a broken reproducer and misclassified it as a 
Clang bug, <https://bugs.llvm.org/show_bug.cgi?id=41896> "Bogus 'error: 
no return statement in constexpr function' when void return type is 
'templated'".  But what apparently happens is that since Clang knows 
that the constexpr function must have a literal return type (which can't 
be void for -std=c++11), it just issues that error whenever it comes a 
constexpr function that lacks a return type.)

> +    {

> +      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

>
Jonathan Wakely May 17, 2019, 9:15 a.m. | #2
On 17/05/19 10:49 +0200, Stephan Bergmann wrote:
>On 14/05/2019 17:25, Jonathan Wakely wrote:

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

>

>>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)

>

>I think this is a problem with -std=c++11 (but not -std=c++14) where 

>void is not yet a literal type, so this function can't be constexpr?



Yes, Clang diagnoses this in C++11 mode but G++ accepts it (unless you
actually try to call g<T>() in a constant expression):

template<typename T> struct voidy { using type = void; };
template<typename T> constexpr typename voidy<T>::type g() {}

$ clang++ v.cc -std=c++11
v.cc:2:56: error: no return statement in constexpr function
template<typename T> constexpr typename voidy<T>::type g() {}
                                                       ^
1 error generated.


I'm testing the attached fix. Thanks for the report.
commit 6ae6283255f58ad44a159970dce6f431cf46f293
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri May 17 10:10:10 2019 +0100

    Fix __invoke_r<void> to be valid in C++11
    
            * include/bits/invoke.h [__cplusplus < 201703L] (__invoke_r<void>):
            Use _GLIBCXX14_CONSTEXPR because void functions cannot be constexpr
            in C++11.

diff --git a/libstdc++-v3/include/bits/invoke.h b/libstdc++-v3/include/bits/invoke.h
index 59e22da84d4..b2e9eee1a48 100644
--- a/libstdc++-v3/include/bits/invoke.h
+++ b/libstdc++-v3/include/bits/invoke.h
@@ -144,7 +144,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   // INVOKE<R> when R is cv void
   template<typename _Res, typename _Callable, typename... _Args>
-    constexpr __can_invoke_as_void<_Res, _Callable, _Args...>
+    _GLIBCXX14_CONSTEXPR __can_invoke_as_void<_Res, _Callable, _Args...>
     __invoke_r(_Callable&& __fn, _Args&&... __args)
     {
       using __result = __invoke_result<_Callable, _Args...>;
Jonathan Wakely May 17, 2019, 9:28 a.m. | #3
On 17/05/19 10:15 +0100, Jonathan Wakely wrote:
>I'm testing the attached fix. Thanks for the report.


Tests passed, patch committed to trunk.


>commit 6ae6283255f58ad44a159970dce6f431cf46f293

>Author: Jonathan Wakely <jwakely@redhat.com>

>Date:   Fri May 17 10:10:10 2019 +0100

>

>    Fix __invoke_r<void> to be valid in C++11

>    

>            * include/bits/invoke.h [__cplusplus < 201703L] (__invoke_r<void>):

>            Use _GLIBCXX14_CONSTEXPR because void functions cannot be constexpr

>            in C++11.

>

>diff --git a/libstdc++-v3/include/bits/invoke.h b/libstdc++-v3/include/bits/invoke.h

>index 59e22da84d4..b2e9eee1a48 100644

>--- a/libstdc++-v3/include/bits/invoke.h

>+++ b/libstdc++-v3/include/bits/invoke.h

>@@ -144,7 +144,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION

> 

>   // INVOKE<R> when R is cv void

>   template<typename _Res, typename _Callable, typename... _Args>

>-    constexpr __can_invoke_as_void<_Res, _Callable, _Args...>

>+    _GLIBCXX14_CONSTEXPR __can_invoke_as_void<_Res, _Callable, _Args...>

>     __invoke_r(_Callable&& __fn, _Args&&... __args)

>     {

>       using __result = __invoke_result<_Callable, _Args...>;

Patch

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