From patchwork Fri Jan 12 09:48:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 187707 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2614:b0:101:6a76:bbe3 with SMTP id mm20csp105415dyc; Fri, 12 Jan 2024 03:22:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEfITMMW9YEyn7tVzd2/QyLawZv8kCg4XIvHGGG0IFFYRbP9m9yDxcTlgxZKKj1jrWtFyoa X-Received: by 2002:a0c:f414:0:b0:67a:b622:60b8 with SMTP id h20-20020a0cf414000000b0067ab62260b8mr703325qvl.61.1705058575384; Fri, 12 Jan 2024 03:22:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705058575; cv=pass; d=google.com; s=arc-20160816; b=mNV+mkcFf/9UK/nERa7JQMRwS0QbmfWw9uOM7OLVHOvj+7qhFVL4c371GPyd6sF71b uy9uvdUB4G4xdE0d+k+xY6Sh69Bf9GaU51E8jCVpFuyPSgPzeqwEJpDAf+VTVA/LOgLs SpCD89sJC9ySWVKmKjLe4pIBpqubDQXm8U5wY8VJgz5bQGKMGHZjfpVZDYzBZgvKM7lp x2Axcl1Rs1E+LzMFUD7H6DI84dTH4F6iFLdJgFgb9ZxL1Lp+j0q0jqcOeHwmM0Dhcg9v 9D5w2hMpahesOI+qL/wFRflSURvS27LpFD/fOMFAHUny0QP9qcesyoW0wk0yCKchAKoX UW2w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=Io/5Z8mR+IJ9BrT7z4wIYPfTo8gFS2fDDKMzm3wcaV0=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=EhCA0Lt+4xjIm2gIk/oMfeuaeXZ00tBYRY/gSchU7zerGlsyPEixBD5/hxZqcTV8mo hs01OkdR2paIq9csPMg0eJqfeRfmWJmLpbggOcTPEoQ8BEPhun8lm447RIeIel0hf4Km aQBVs0DDpuwmIOrifTHgGLagoU9sGy1kn3VtfRmbIf1V+ZQ2HWyE5X4A6V0hsuWQJarJ cPuAr/t2hoYmocMNEGs/4Qcklzd+MecRvRVXw3a/ibHOeJBE7UUqCjj8LbZei23YO5RI 17nM/1zlchxEPuKgit+4WbuehmSU1oHvUCZ+LTFB3jvyBNdc3BKFFokKZuzfGivxBN2g x+1w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FNeMJNpC; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id e1-20020a0cf341000000b0065ae2cc68f3si2654076qvm.387.2024.01.12.03.22.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 03:22:55 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FNeMJNpC; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B28063860768 for ; Fri, 12 Jan 2024 09:51:05 +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 AC430385DC01 for ; Fri, 12 Jan 2024 09:49:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC430385DC01 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 AC430385DC01 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=1705052947; cv=none; b=C2FpFEUr0EtmlzCeaiLX84VEOrAZq1msUvm9q5eC5+7/7NdbrYcLpAaqYx2SlYotCXRqgsgOxLkoMyxvTEdzIKFExqfV8Rgkt78n7F53IAKWzTeRk5SRyz7s2r53H1ZKM/UDS0Q37XT1HRPY2A0bolzt1IBM5tQvYWkRjkV3AWs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705052947; c=relaxed/simple; bh=CXqIZDy/mvO295+U5JYsIgK2vISwpKf08uwGuwNGm0o=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=NUCKcpVLKBpty1HZo8yJrRxGLuGvmFK4mKVlENDAEgKbGSjFfUyrcF1S6tj1gc8BNjKyGasDGHp1uaHcYdyf0LhuMvZlEXG37lwt4Dxvt5C2R0SKhFQ4Pr+B8ppQ0D28GZZVmRqa2RoHSJaXC54A6h2Gs1vZl6D0Vs75uI9jvLA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705052945; 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; bh=Io/5Z8mR+IJ9BrT7z4wIYPfTo8gFS2fDDKMzm3wcaV0=; b=FNeMJNpCZEv9ouIaOXb6+39nQJhNUgo+FGezZg5PYf4X5wWYrne07rWfthy6BJ9wseOHea 5mz5QU88oAg4ft3R2aUEsNCgpUX0L7AxjPV/17kfgsAldURWsJ3LEmclqJTQOgxv4IPv/6 VqKuZ8Cb0yz4CUer0rTA0XnXZG8ws5s= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-562-_0nkbVlVPLmIPpJT-9mifg-1; Fri, 12 Jan 2024 04:48:57 -0500 X-MC-Unique: _0nkbVlVPLmIPpJT-9mifg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id BA9C53C00097; Fri, 12 Jan 2024 09:48:56 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 86113C19060; Fri, 12 Jan 2024 09:48:56 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Implement C++23 P1951R1 (Default Args for pair's Forwarding Ctor) [PR105505] Date: Fri, 12 Jan 2024 09:48:20 +0000 Message-ID: <20240112094855.684825-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 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_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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: 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787883500781633749 X-GMAIL-MSGID: 1787883500781633749 Tested aarch64-linux, pushed to trunk. -- >8 -- This was approved for C++23 at he June 2021 virtual meeting. This allows more efficient construction of std::pair members when {} is used as a constructor argument. The perfect forwarding constructor can be used, so that the member variables are constructed from forwarded arguments instead of being copy constructed from temporaries. libstdc++-v3/ChangeLog: PR libstdc++/105505 * include/bits/stl_pair.h (pair::pair(U1&&, U2&&)) [C++23]: Add default template arguments, as per P1951R1. * testsuite/20_util/pair/cons/default_tmpl_args.cc: New test. --- libstdc++-v3/include/bits/stl_pair.h | 8 ++++ .../20_util/pair/cons/default_tmpl_args.cc | 48 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 libstdc++-v3/testsuite/20_util/pair/cons/default_tmpl_args.cc diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h index ce8826ae6a8..52f532f3c39 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -308,7 +308,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { } /// Constructor accepting two values of arbitrary types +#if __cplusplus > 202002L + template +#else template +#endif requires (_S_constructible<_U1, _U2>()) && (!_S_dangles<_U1, _U2>()) constexpr explicit(!_S_convertible<_U1, _U2>()) pair(_U1&& __x, _U2&& __y) @@ -316,7 +320,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } +#if __cplusplus > 202002L + template +#else template +#endif requires (_S_constructible<_U1, _U2>()) && (_S_dangles<_U1, _U2>()) constexpr explicit(!_S_convertible<_U1, _U2>()) pair(_U1&&, _U2&&) = delete; diff --git a/libstdc++-v3/testsuite/20_util/pair/cons/default_tmpl_args.cc b/libstdc++-v3/testsuite/20_util/pair/cons/default_tmpl_args.cc new file mode 100644 index 00000000000..5960bf72e21 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/pair/cons/default_tmpl_args.cc @@ -0,0 +1,48 @@ +// { dg-do compile { target c++23 } } + +// P1951R1 Default Arguments for pair's Forwarding Constructor + +#include +#include +#include +#include + +void +test_p1951r1_example() +{ + std::pair> p("hello", {}); +} + +struct Counter +{ + constexpr Counter() = default; + constexpr Counter(int) { } + constexpr Counter(const Counter& c) : copies(c.copies + 1), moves(c.moves) { } + constexpr Counter(Counter&& c) : copies(c.copies), moves(c.moves+1) { } + int copies = 0; + int moves = 0; +}; + +constexpr bool +test_count_copies() +{ + std::pair p1(1, {}); + VERIFY( p1.first.copies == 0 && p1.second.copies == 0 ); + VERIFY( p1.first.moves == 0 && p1.second.moves == 1 ); + + std::pair p2({}, 1); + VERIFY( p2.first.copies == 0 && p2.second.copies == 0 ); + VERIFY( p2.first.moves == 1 && p2.second.moves == 0 ); + + std::pair p3({}, {}); + VERIFY( p3.first.copies == 0 && p3.second.copies == 0 ); + VERIFY( p3.first.moves == 1 && p3.second.moves == 1 ); + + return true; +} + +int main() +{ + test_p1951r1_example(); + static_assert( test_count_copies() ); +}