]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::is_nothrow_invocable_r for uncopyable prvalues [PR91456]
authorJonathan Wakely <jwakely@redhat.com>
Fri, 23 Sep 2022 12:28:37 +0000 (13:28 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 16 May 2023 11:14:18 +0000 (12:14 +0100)
This is the last missing piece of PR 91456.

This also removes the only use of the C++11 version of
std::is_nothrow_invocable.

libstdc++-v3/ChangeLog:

PR libstdc++/91456
* include/std/type_traits (__is_nothrow_invocable): Remove.
(__is_invocable_impl::__nothrow_type): New member type which
checks if the conversion can throw.
(__is_nt_invocable_impl): Replace class template with alias
template to __is_nt_invocable_impl::__nothrow_type.
* testsuite/20_util/is_nothrow_invocable/91456.cc: New test.
* testsuite/20_util/is_nothrow_convertible/value.cc: Remove
macro used by value_ext.cc test.
* testsuite/20_util/is_nothrow_convertible/value_ext.cc: Remove
test for non-standard __is_nothrow_invocable trait.

(cherry picked from commit 71c828f84572d933979468baf2cf744180258ee4)

libstdc++-v3/include/std/type_traits
libstdc++-v3/testsuite/20_util/is_nothrow_convertible/value.cc
libstdc++-v3/testsuite/20_util/is_nothrow_invocable/91456.cc [moved from libstdc++-v3/testsuite/20_util/is_nothrow_convertible/value_ext.cc with 59% similarity]

index cccc6eed7632dc8663829c572d37844d5c41b60b..d544759f28e8c2ead1279ae95f2a5ac0d4ad507f 100644 (file)
@@ -1492,12 +1492,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     };
 #pragma GCC diagnostic pop
 
-  // is_nothrow_convertible for C++11
-  template<typename _From, typename _To>
-    struct __is_nothrow_convertible
-    : public __is_nt_convertible_helper<_From, _To>::type
-    { };
-
 #if __cplusplus > 201703L
 #define __cpp_lib_is_nothrow_convertible 201806L
   /// is_nothrow_convertible
@@ -2881,7 +2875,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   // The primary template is used for invalid INVOKE expressions.
   template<typename _Result, typename _Ret,
           bool = is_void<_Ret>::value, typename = void>
-    struct __is_invocable_impl : false_type { };
+    struct __is_invocable_impl
+    : false_type
+    {
+      using __nothrow_type = false_type; // For is_nothrow_invocable_r
+    };
 
   // Used for valid INVOKE and INVOKE<void> expressions.
   template<typename _Result, typename _Ret>
@@ -2889,7 +2887,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                               /* is_void<_Ret> = */ true,
                               __void_t<typename _Result::type>>
     : true_type
-    { };
+    {
+      using __nothrow_type = true_type; // For is_nothrow_invocable_r
+    };
 
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wctor-dtor-privacy"
@@ -2901,23 +2901,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
     private:
       // The type of the INVOKE expression.
-      // Unlike declval, this doesn't add_rvalue_reference.
-      static typename _Result::type _S_get();
+      // Unlike declval, this doesn't add_rvalue_reference, so it respects
+      // guaranteed copy elision.
+      static typename _Result::type _S_get() noexcept;
 
       template<typename _Tp>
-       static void _S_conv(_Tp);
+       static void _S_conv(_Tp) noexcept;
 
       // This overload is viable if INVOKE(f, args...) can convert to _Tp.
-      template<typename _Tp, typename = decltype(_S_conv<_Tp>(_S_get()))>
-       static true_type
+      template<typename _Tp, bool _Check_Noex = false,
+              typename = decltype(_S_conv<_Tp>(_S_get())),
+              bool _Noex = noexcept(_S_conv<_Tp>(_S_get()))>
+       static __bool_constant<_Check_Noex ? _Noex : true>
        _S_test(int);
 
-      template<typename _Tp>
+      template<typename _Tp, bool = false>
        static false_type
        _S_test(...);
 
     public:
+      // For is_invocable_r
       using type = decltype(_S_test<_Ret>(1));
+
+      // For is_nothrow_invocable_r
+      using __nothrow_type = decltype(_S_test<_Ret, true>(1));
     };
 #pragma GCC diagnostic pop
 
@@ -3048,15 +3055,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     };
 
   /// @cond undocumented
-  template<typename _Result, typename _Ret, typename = void>
-    struct __is_nt_invocable_impl : false_type { };
-
   template<typename _Result, typename _Ret>
-    struct __is_nt_invocable_impl<_Result, _Ret,
-                                 __void_t<typename _Result::type>>
-    : __or_<is_void<_Ret>,
-           __is_nothrow_convertible<typename _Result::type, _Ret>>
-    { };
+    using __is_nt_invocable_impl
+      = typename __is_invocable_impl<_Result, _Ret>::__nothrow_type;
   /// @endcond
 
   /// std::is_nothrow_invocable_r
index 22d7c8c1df69a89c53758b320843f966078547a6..18dd6a5e5dd4dc361a4c2e13e39169ac0541edf0 100644 (file)
@@ -21,9 +21,7 @@
 #include <type_traits>
 #include <testsuite_tr1.h>
 
-#ifndef IS_NT_CONVERTIBLE_DEFINED
 using std::is_nothrow_convertible;
-#endif
 
 void test01()
 {
similarity index 59%
rename from libstdc++-v3/testsuite/20_util/is_nothrow_convertible/value_ext.cc
rename to libstdc++-v3/testsuite/20_util/is_nothrow_invocable/91456.cc
index 9c4d19ef5ab3094c9755773edcfae12180a73935..574233986f8fa5106b7d271ad6037acd6b28d355 100644 (file)
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-do compile { target c++11 } }
+// { dg-do compile { target c++17 } }
+
+// PR 91456
+// std::function and std::is_invocable_r do not understand guaranteed elision
 
 #include <type_traits>
 
-// Test the non-standard __is_nothrow_convertible trait
+#include <functional>
 
-template<typename From, typename To>
-  using is_nothrow_convertible = std::__is_nothrow_convertible<From, To>;
+struct Immovable {
+  Immovable() = default;
+  Immovable(const Immovable&) = delete;
+  Immovable& operator=(const Immovable&) = delete;
+};
 
-#define IS_NT_CONVERTIBLE_DEFINED
-#include "value.cc"
+static_assert(std::is_nothrow_invocable_r_v<Immovable, Immovable(*)() noexcept>);
+static_assert(std::is_nothrow_invocable_r_v<const Immovable, Immovable(*)() noexcept>);
+static_assert(std::is_nothrow_invocable_r_v<Immovable, const Immovable(*)() noexcept>);