From patchwork Tue Jul 16 11:56:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1961059 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QZonBXhQ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WNd7k6NHZz1xrQ for ; Tue, 16 Jul 2024 22:05:54 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2D0EB3839163 for ; Tue, 16 Jul 2024 12:05:53 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 26D39384A43F for ; Tue, 16 Jul 2024 12:04:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 26D39384A43F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 26D39384A43F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721131497; cv=none; b=l/1eZAiFSwJQ6quy9mTeaz62P3yFTAqNOn0/gUqzgI8OlWmBOKkZrbc5Bmr6qaoUc4FRqTlPxRCdXuY7AALLyr4QsvgeEpsh9X5LFHcIGpwNXe1oHLgSLnYLLr8jTxRYsT+MsPfZLYaTiSOJKnE2G67hPlDAlNKilV9N8uHs04w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721131497; c=relaxed/simple; bh=jnxru0QWtQ6L80pS61RIda8IjunwVfBtaDj/uJ5Xh+8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fUyqqscaP3y5XqgHHf12ODt2g970vEUiTCJZc/ND4JqtuplRMeVLvS6mkDOqAYUkH7/X4e5Xc+PA74Z7rKTiZhB3oHtOGe3GKEKIe76Ci27GPm0P9KrO4fU+NGp418Gck/AU6siQoitf3Q60F5cYincD1CHCZ7lIxg9VhPLK12Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721131488; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EZIUGjidN8i2i/fHJRHUnPxu/s5eFG7E5cP2oEbWCcM=; b=QZonBXhQ3reYSJXfAAThbFeDx1QM/e/6lDlzfdgLmdlVAB+NuSC0keWUHO+h/BwoVeHw5k s9/Dzb8wfMwcUigJZbg4YI5vYSeD/sbZYbCPN9AL3+BuzBzB1WTxzA2k8+qkO0CQQnT5oq DM6iK53r8n/C6AF/db5BklM8grpuSEY= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-678-RdAoPgp7NLS6C4SbpafaqQ-1; Tue, 16 Jul 2024 08:04:47 -0400 X-MC-Unique: RdAoPgp7NLS6C4SbpafaqQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4104A1955D52; Tue, 16 Jul 2024 12:04:46 +0000 (UTC) Received: from localhost (unknown [10.42.28.14]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6EF5A1955D42; Tue, 16 Jul 2024 12:04:45 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 4/6] libstdc++: Define C++26 member visit for std::variant [PR110356] Date: Tue, 16 Jul 2024 12:56:00 +0100 Message-ID: <20240716120436.2135312-4-jwakely@redhat.com> In-Reply-To: <20240716120436.2135312-1-jwakely@redhat.com> References: <20240716120436.2135312-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. -- >8 -- Implement the std::variant changes from P2637R3. libstdc++-v3/ChangeLog: PR libstdc++/110356 * include/bits/version.def (variant): Update for C++26. * include/bits/version.h: Regenerate. * include/std/variant (variant::visit): New member functions. * testsuite/20_util/variant/visit.cc: Check second alternative. * testsuite/20_util/variant/visit_member.cc: New test. --- libstdc++-v3/include/bits/version.def | 5 + libstdc++-v3/include/bits/version.h | 7 +- libstdc++-v3/include/std/variant | 48 +++++++ .../testsuite/20_util/variant/visit.cc | 5 +- .../testsuite/20_util/variant/visit_member.cc | 117 ++++++++++++++++++ 5 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/variant/visit_member.cc diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index bcb33c18aa4..806f1e9549b 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -478,6 +478,11 @@ ftms = { ftms = { name = variant; + values = { + v = 202306; + cxxmin = 26; + extra_cond = "__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L && __cpp_explicit_this_parameter"; + }; values = { v = 202106; cxxmin = 20; diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index 4d1af34bf8d..e8ca0faf5dc 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -529,7 +529,12 @@ #undef __glibcxx_want_type_trait_variable_templates #if !defined(__cpp_lib_variant) -# if (__cplusplus >= 202002L) && (__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L) +# if (__cplusplus > 202302L) && (__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L && __cpp_explicit_this_parameter) +# define __glibcxx_variant 202306L +# if defined(__glibcxx_want_all) || defined(__glibcxx_want_variant) +# define __cpp_lib_variant 202306L +# endif +# elif (__cplusplus >= 202002L) && (__cpp_concepts >= 202002L && __cpp_constexpr >= 201811L) # define __glibcxx_variant 202106L # if defined(__glibcxx_want_all) || defined(__glibcxx_want_variant) # define __cpp_lib_variant 202106L diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 3a23d9bc66d..154aab98812 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -1390,6 +1390,12 @@ namespace __detail::__variant constexpr __detail::__variant::__visit_result_t<_Visitor, _Variants...> visit(_Visitor&&, _Variants&&...); +#if __cplusplus > 201703L + template + constexpr _Res + visit(_Visitor&&, _Variants&&...); +#endif + template _GLIBCXX20_CONSTEXPR inline enable_if_t<(is_move_constructible_v<_Types> && ...) @@ -1758,6 +1764,48 @@ namespace __detail::__variant }, __rhs); } +#if __cpp_lib_variant >= 202306L // >= C++26 + // [variant.visit], visitation + + /** Simple visitation for a single variant + * + * To visit a single variant you can use `var.visit(visitor)` + * instead of `std::visit(visitor, var)`. + * + * @since C++26 + */ + template + constexpr decltype(auto) + visit(this _Self&& __self, _Visitor&& __vis) + { + using _CVar = __conditional_t>, + const variant, variant>; + using _Var = __conditional_t, + _CVar&&, _CVar&>; + static_assert(is_same_v<__like_t<_Self, variant>, _Var>); + return std::visit(std::forward<_Visitor>(__vis), (_Var)__self); + } + + /** Simple visitation for a single variant, with explicit return type + * + * To visit a single variant you can use `var.visit(visitor)` + * instead of `std::visit(visitor, var)`. + * + * @since C++26 + */ + template + constexpr _Res + visit(this _Self&& __self, _Visitor&& __vis) + { + using _CVar = __conditional_t>, + const variant, variant>; + using _Var = __conditional_t, + _CVar&&, _CVar&>; + static_assert(is_same_v<__like_t<_Self, variant>, _Var>); + return std::visit<_Res>(std::forward<_Visitor>(__vis), (_Var)__self); + } +#endif + private: template friend constexpr decltype(auto) diff --git a/libstdc++-v3/testsuite/20_util/variant/visit.cc b/libstdc++-v3/testsuite/20_util/variant/visit.cc index 7f79e6107ab..6edc7d7c028 100644 --- a/libstdc++-v3/testsuite/20_util/variant/visit.cc +++ b/libstdc++-v3/testsuite/20_util/variant/visit.cc @@ -18,7 +18,7 @@ // { dg-do run { target c++17 } } #include -#include +#include // reference_wrapper #include // N.B. there are more std::visit tests in ./compile.cc and ./run.cc @@ -84,6 +84,9 @@ test02() // Visit should not need arguments to be copyable: int res = std::visit(f, v); VERIFY( res == 1 ); + v.emplace(); + res = std::visit(f, v); + VERIFY( res == 0 ); } int diff --git a/libstdc++-v3/testsuite/20_util/variant/visit_member.cc b/libstdc++-v3/testsuite/20_util/variant/visit_member.cc new file mode 100644 index 00000000000..90332b94c72 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/variant/visit_member.cc @@ -0,0 +1,117 @@ +// Copyright (C) 2019-2024 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 +// . + +// { dg-do run { target c++26 } } + +#include + +#if __cpp_lib_variant < 202306L +# error __cpp_lib_variant has the wrong value in +#endif + +#include // reference_wrapper +#include + +void +test01() +{ + // Verify that visitation uses INVOKE and supports a pointer-to-member. + struct X { int n; }; + using V = std::variant>; + struct Derv : private V { + using V::V; + using V::visit; + }; + + Derv v{X{1}}; + static_assert( std::is_same_v ); + + // Verify that constness and value category are correctly forwarded. + std::variant v2{1}; + auto id = [](T&& t) -> T&& { return std::forward(t); }; + static_assert( std::is_same_v ); + static_assert( std::is_same_v ); + const auto& vc = v2; + static_assert( std::is_same_v ); + static_assert( std::is_same_v ); + + static_assert( std::is_same_v(id)), void> ); +} + +void +test02() +{ + struct NoCopy + { + NoCopy() { } + NoCopy(const NoCopy&) = delete; + NoCopy(NoCopy&&) = delete; + ~NoCopy() { } + + int operator()(int i) { return i; } + int operator()(const NoCopy&) { return 100; } + }; + + std::variant v{10}; + NoCopy f; + // Visit should not need arguments to be copyable: + int res = v.visit(f); + VERIFY( res == 10 ); + v.emplace(); + res = v.visit(f); + VERIFY( res == 100 ); + res = v.visit(f); + VERIFY( res == 1 ); +} + +void +test03() +{ + // Verify that member visit can access the variant as a private base class. + struct Derived : private std::variant + { + using variant::visit; + }; + Derived d; + int i = d.visit([](int& x) { return --x; }); + VERIFY( i == -1 ); + unsigned u = d.visit([](int x) { return x; }); + VERIFY( u == -1u ); +} + +void +test04() +{ + struct A { char a = 'a'; }; + struct B { char b = 'b'; }; + struct C { char c = 'c'; }; + auto f = [](auto x) { return B{}; }; + using State = std::variant; + State state = A{}; + auto res = std::move(state).visit(f); + // Verify that visit only matches the explicit return type overload. + static_assert( std::is_same_v ); +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); +}