]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::array<T, 0>::data() to be a constant expression [PR108258]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 4 Jan 2023 11:49:19 +0000 (11:49 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 4 Jan 2023 11:53:49 +0000 (11:53 +0000)
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().

libstdc++-v3/include/std/array
libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc

index e26390e6f80503e59c6c98c6dab46678fb33cb17..c50a201b0325d3a7ecb7d602a9b150a79b6509bc 100644 (file)
@@ -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;
index b6878fd0c59a62e7d1878d2c5d098698fa52d98c..b92aa5c04e23bc3ab9cf98a6561ad0e57897d696 100644 (file)
@@ -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() );