public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-8711] libstdc++: Fix visit<void>(v) for non-void visitors [PR106589]
Date: Wed, 24 Aug 2022 13:01:38 +0000 (GMT)	[thread overview]
Message-ID: <20220824130138.1F5D7385C335@sourceware.org> (raw)

https://gcc.gnu.org/g:219d9f61a241d370b7933aecae56ce0905465830

commit r12-8711-g219d9f61a241d370b7933aecae56ce0905465830
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Aug 23 15:46:16 2022 +0100

    libstdc++: Fix visit<void>(v) for non-void visitors [PR106589]
    
    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).
    
    (cherry picked from commit e85bb1881e57e53306ede2a15f30d06480d69886)

Diff:
---
 libstdc++-v3/include/std/variant                  | 7 ++++++-
 libstdc++-v3/testsuite/20_util/variant/visit_r.cc | 8 ++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index beed396fccb..df0dd08d90a 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -1730,7 +1730,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."
diff --git a/libstdc++-v3/testsuite/20_util/variant/visit_r.cc b/libstdc++-v3/testsuite/20_util/variant/visit_r.cc
index 712459f25e3..c77b259c386 100644
--- a/libstdc++-v3/testsuite/20_util/variant/visit_r.cc
+++ b/libstdc++-v3/testsuite/20_util/variant/visit_r.cc
@@ -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();
 }

                 reply	other threads:[~2022-08-24 13:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220824130138.1F5D7385C335@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).