From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 4BD9338582B2 for ; Tue, 23 Aug 2022 15:39:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4BD9338582B2 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-301-39s311x6NDOfN6ie_a6PMw-1; Tue, 23 Aug 2022 11:39:13 -0400 X-MC-Unique: 39s311x6NDOfN6ie_a6PMw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6117A18A01C0; Tue, 23 Aug 2022 15:39:13 +0000 (UTC) Received: from localhost (unknown [10.33.36.78]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28D972026D4C; Tue, 23 Aug 2022 15:39:13 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix visit(v) for non-void visitors [PR106589] Date: Tue, 23 Aug 2022 16:39:12 +0100 Message-Id: <20220823153912.229679-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 23 Aug 2022 15:39:18 -0000 Tested powerpc64le-linux, pushed to trunk. Backport to gcc-12 needed. -- >8 -- 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 for zero argument case. * testsuite/20_util/variant/visit_r.cc: Check std::visit(v). --- 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 f8f15665433..c234b54421e 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -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." 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(Visitor(), v); } +void test03() +{ + // PR libstdc++/106589 - visit rejects lambdas that do not return void + auto visitor = []{ return 0; }; + std::visit(visitor); + std::visit(static_cast(visitor)); +} int main() { test01(); test02(); + test03(); } -- 2.37.2