[libcxx-commits] [libcxx] [libc++] Fix constexpr initialization of std::array<T, 0> (PR #74667)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 14 06:58:25 PST 2023


================
@@ -0,0 +1,142 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <array>
+
+// template <class T, size_t N>
+// struct array
+
+// Make sure std::array<T, N> has the correct object size and alignment.
+// This test is mostly meant to catch subtle ABI-breaking regressions.
+
+// Ignore error about requesting a large alignment not being ABI compatible with older AIX systems.
+#if defined(_AIX)
+#  pragma clang diagnostic ignored "-Waix-compat"
+#endif
+
+#include <array>
+#include <cstddef>
+#include <type_traits>
+
+#include "test_macros.h"
+
+template <class T, std::size_t Size>
+struct MyArray {
+  T elems[Size];
+};
+
+template <class T>
+void test_type() {
+  {
+    using Array = std::array<T, 0>;
+    static_assert(sizeof(Array) == sizeof(T), "");
+    static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(T), "");
+    static_assert(sizeof(Array) == sizeof(T[1]), "");
+    static_assert(sizeof(Array) == sizeof(MyArray<T, 1>), "");
+    static_assert(TEST_ALIGNOF(Array) == TEST_ALIGNOF(MyArray<T, 1>), "");
+    static_assert(!std::is_empty<Array>::value, "");
----------------
ldionne wrote:

cppreference says:

> This container is an aggregate type with the same semantics as a struct holding a C-style array `T[N]` as its only non-static data member.

I think it's reasonable (and helpful for other implementations) to have these checks enabled for all implementations.

The tests for the 0-sized specialization could potentially be made libc++ only, but then again I think this will help enforce that all implementations provide compatible implementations.

https://github.com/llvm/llvm-project/pull/74667


More information about the libcxx-commits mailing list