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 r13-4991] libstdc++: Fix std::array<T, 0>::data() to be a constant expression [PR108258]
Date: Wed,  4 Jan 2023 11:54:23 +0000 (GMT)	[thread overview]
Message-ID: <20230104115423.10F363858D1E@sourceware.org> (raw)

https://gcc.gnu.org/g:1530a9b1f45a7ceb333f3e1abad65e587679690f

commit r13-4991-g1530a9b1f45a7ceb333f3e1abad65e587679690f
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jan 4 11:49:19 2023 +0000

    libstdc++: Fix std::array<T, 0>::data() to be a constant expression [PR108258]
    
    When I refactored the __array_traits helper I broke this.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/108258
            * include/std/array (__array_traits<T, 0>::operator T*()): Add
            constexpr.
            * testsuite/23_containers/array/element_access/constexpr_c++17.cc: Check
            std::array<T, 0>::data().

Diff:
---
 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<std::size_t, 6> 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<int, 0> 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() );

                 reply	other threads:[~2023-01-04 11:54 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=20230104115423.10F363858D1E@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).