[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 14:51:15 PST 2023


================
@@ -18,26 +18,33 @@ struct NoDefault {
     TEST_CONSTEXPR NoDefault(int) { }
 };
 
-// Test default initialization
-// This one isn't constexpr because omitting to initialize fundamental types
-// isn't valid in a constexpr context.
-struct test_default_initialization {
+struct test_initialization {
     template <typename T>
-    void operator()() const
+    TEST_CONSTEXPR_CXX14 void operator()() const
     {
-        std::array<T, 0> a0; (void)a0;
-        std::array<T, 1> a1; (void)a1;
-        std::array<T, 2> a2; (void)a2;
-        std::array<T, 3> a3; (void)a3;
+        // Check default initalization
+        {
+            std::array<T, 0> a0; (void)a0;
+            // Before C++20, default initialization doesn't work inside constexpr for
+            // trivially default constructible types. This only apply to non-empty arrays,
+            // since empty arrays don't hold an element of type T.
+            if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED || !std::is_trivially_default_constructible<T>::value) {
----------------
ldionne wrote:

I know -- I started like that and it was unreadable since I could hardly reuse anything at all, and I'm spamming exactly the same test over and over again for a bunch of different types. I dislike logic in tests, but I think this strikes the right balance after spending a bunch of time investigating different approaches.

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


More information about the libcxx-commits mailing list