From patchwork Wed Jan 4 11:54:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 38879 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp5101819wrt; Wed, 4 Jan 2023 03:55:45 -0800 (PST) X-Google-Smtp-Source: AMrXdXuoygWEURCYtoT5HX3gD9ewII2cnX4zT5O8VO2NX7BuaM7upKzpeRJx39a0eDqwrsOi/Nms X-Received: by 2002:a17:906:5dd2:b0:7d5:29e1:15ea with SMTP id p18-20020a1709065dd200b007d529e115eamr41766796ejv.8.1672833344988; Wed, 04 Jan 2023 03:55:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672833344; cv=none; d=google.com; s=arc-20160816; b=mGWnLp22KeEp53Iv6nKU4KAF+fWm1ya3wP3AKElSDCZrdqBxspn7tEmE8x6xTjzlaC 40pLqeexNfY+ic3ZPm0mhyH96nNbuHN5D0CYr092MVIPqTORxOFjv0srVXsAMWR6EFjy FBUAoJ5DAAeK03Wrou+inRm4SgiorcCIwlAm54D/KqUIF+bwpGlUiH5zXnPRosG7/bAC Wxg7HWan2yb0QXOUAx4NAk6Ggj/x2lPrl4sxhK9EZf+Dt+txsuQW2OL70P3scm09EZGB q+jklnyf5e7qys5cqUiMxddL45jiPEreduuYNpol2V/DWC1xzBCdktZtBSvMaXNIC4uv K5WQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ZeUr5Ui97hzYqBck5EBYwWWbuPojGjh3oaSaILXx8V4=; b=vQxk6ZdbPpIYiCX6O/YGraR3UqFQNrWaQAAFfMNtI1J0tumUyrLpx0dBEmNqcvjT8m 8pa1PB9ZRZeWH51gfe3/VxSnB0wX4C2VK1E6U1gcgz9lLHP7sjNP1nuiBUbyfRl2MLZN jCupxhIv7/yQBnImJuQu9qS+6u7HiPYtOwPvL4rJquBw2dLCtF1pmshz7yq8DrMRC3B1 WYf0yVBbi26yt9QVwnap42UuL06GziHhD7ZEOJLEvx2ehWnRGN/JnUK11jiWWOcWNWLt /nSBUHDnWt/q1lmy3IshC3DvXGhaGMUqQsKpf70VSUzaezEiCKQyjZFAAt2EMlam8Zu4 2fqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=UAQualDz; 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=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ho6-20020a1709070e8600b007ae186b15f9si14340005ejc.597.2023.01.04.03.55.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 03:55:44 -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=@gcc.gnu.org header.s=default header.b=UAQualDz; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7CC43385B524 for ; Wed, 4 Jan 2023 11:55:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CC43385B524 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672833325; bh=ZeUr5Ui97hzYqBck5EBYwWWbuPojGjh3oaSaILXx8V4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=UAQualDzRentd7GvzzsBU4ydBRpVZhaiIn5RaF/3LQMvibBOC2XP0qMocQmzrbl8s oIC0Z0CmJX9tY/uuAPgz2hnn/rLTDu+QBZ94iJHsUhEYFy4Cws08j24cXNiatx4C2k wXfbT8v/5Utmb2kN72+2zMC9u9jlorml8bg6ATl8= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 179803858C30 for ; Wed, 4 Jan 2023 11:54:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 179803858C30 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-91-PSQaLqFLO0yy4nK_53uAlw-1; Wed, 04 Jan 2023 06:54:38 -0500 X-MC-Unique: PSQaLqFLO0yy4nK_53uAlw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 60AB780D0E5; Wed, 4 Jan 2023 11:54:38 +0000 (UTC) Received: from localhost (unknown [10.33.36.242]) by smtp.corp.redhat.com (Postfix) with ESMTP id 27772C15BA0; Wed, 4 Jan 2023 11:54:38 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::array::data() to be a constant expression [PR108258] Date: Wed, 4 Jan 2023 11:54:37 +0000 Message-Id: <20230104115437.48991-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754092897178115450?= X-GMAIL-MSGID: =?utf-8?q?1754092897178115450?= Tested x86_64-linux. Pushed to trunk. -- >8 -- When I refactored the __array_traits helper I broke this. libstdc++-v3/ChangeLog: PR libstdc++/108258 * include/std/array (__array_traits::operator T*()): Add constexpr. * testsuite/23_containers/array/element_access/constexpr_c++17.cc: Check std::array::data(). --- libstdc++-v3/include/std/array | 2 +- .../array/element_access/constexpr_c++17.cc | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index e26390e6f80..c50a201b032 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -69,7 +69,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Conversion to a pointer produces a null pointer. __attribute__((__always_inline__)) - operator _Tp*() const noexcept { return nullptr; } + constexpr operator _Tp*() const noexcept { return nullptr; } }; using _Is_swappable = true_type; diff --git a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc index b6878fd0c59..b92aa5c04e2 100644 --- a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc +++ b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc @@ -34,21 +34,34 @@ constexpr std::size_t test01() auto v2 = a.at(2); auto v3 = a.front(); auto v4 = a.back(); - return v1 + v2 + v3 + v4; + auto v5 = *a.data(); + return v1 + v2 + v3 + v4 + v5; } static_assert( test01() == (55 + 66 + 0 + 2) ); constexpr std::size_t test02() { - // array + // const array typedef std::array array_type; const array_type a = { { 0, 55, 66, 99, 4115, 2 } }; auto v1 = a[1]; auto v2 = a.at(2); auto v3 = a.front(); auto v4 = a.back(); - return v1 + v2 + v3 + v4; + auto v5 = *a.data(); + return v1 + v2 + v3 + v4 + v5; } static_assert( test02() == (55 + 66 + 0 + 2) ); + +constexpr bool test_zero() +{ + // zero-sized array (PR libstdc++/108258) + std::array a{}; + auto v4 = a.data(); + // The standard says this is unspecified, it's null for our implementation: + return a.data() == nullptr; +} + +static_assert( test_zero() );