]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix visit<void>(v) for non-void visitors [PR106589]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 23 Aug 2022 14:46:16 +0000 (15:46 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 23 Aug 2022 15:35:07 +0000 (16:35 +0100)
The optimization for the common case of std::visit forgot to handle the
edge case of passing zero variants to a non-void visitor and converting
the result to void.

libstdc++-v3/ChangeLog:

PR libstdc++/106589
* include/std/variant (__do_visit): Handle is_void<R> for zero
argument case.
* testsuite/20_util/variant/visit_r.cc: Check std::visit<void>(v).

libstdc++-v3/include/std/variant
libstdc++-v3/testsuite/20_util/variant/visit_r.cc

index f8f15665433b9e799c557699eb4c8ab82e605ada..c234b54421e497b7920834096e3d00a45afc6e40 100644 (file)
@@ -1728,7 +1728,12 @@ namespace __variant
     {
       // Get the silly case of visiting no variants out of the way first.
       if constexpr (sizeof...(_Variants) == 0)
-       return std::forward<_Visitor>(__visitor)();
+       {
+         if constexpr (is_void_v<_Result_type>)
+           return (void) std::forward<_Visitor>(__visitor)();
+         else
+           return std::forward<_Visitor>(__visitor)();
+       }
       else
        {
          constexpr size_t __max = 11; // "These go to eleven."
index 712459f25e39e191e04fdd71279a7fcf84a7fca9..c77b259c386b55fe443286ba32e70ae4c81cfc3e 100644 (file)
@@ -54,10 +54,18 @@ void test02()
   std::visit<const void>(Visitor(), v);
 }
 
+void test03()
+{
+  // PR libstdc++/106589 - visit<void> rejects lambdas that do not return void
+  auto visitor = []{ return 0; };
+  std::visit<void>(visitor);
+  std::visit<void>(static_cast<int(*)()>(visitor));
+}
 
 int
 main()
 {
   test01();
   test02();
+  test03();
 }