[libcxx-commits] [libcxx] df6c820 - [libc++] Fix the mdspan ElementType complete object type mandate (#191703)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 13 02:16:49 PDT 2026


Author: eiytoq
Date: 2026-04-13T17:16:44+08:00
New Revision: df6c82053c5e1f9814d130d423f34871bc6423c5

URL: https://github.com/llvm/llvm-project/commit/df6c82053c5e1f9814d130d423f34871bc6423c5
DIFF: https://github.com/llvm/llvm-project/commit/df6c82053c5e1f9814d130d423f34871bc6423c5.diff

LOG: [libc++] Fix the mdspan ElementType complete object type mandate (#191703)

Fixes: #191688

Added: 
    

Modified: 
    libcxx/include/__mdspan/mdspan.h
    libcxx/test/std/containers/views/mdspan/mdspan/element_type.verify.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__mdspan/mdspan.h b/libcxx/include/__mdspan/mdspan.h
index 7f222733cc4c5..024c1002b1a09 100644
--- a/libcxx/include/__mdspan/mdspan.h
+++ b/libcxx/include/__mdspan/mdspan.h
@@ -30,6 +30,7 @@
 #include <__type_traits/is_constructible.h>
 #include <__type_traits/is_convertible.h>
 #include <__type_traits/is_nothrow_constructible.h>
+#include <__type_traits/is_object.h>
 #include <__type_traits/is_pointer.h>
 #include <__type_traits/is_same.h>
 #include <__type_traits/rank.h>
@@ -66,6 +67,9 @@ class mdspan {
 private:
   static_assert(__mdspan_detail::__is_extents_v<_Extents>,
                 "mdspan: Extents template parameter must be a specialization of extents.");
+  static_assert(
+      is_object_v<_ElementType> && requires { sizeof(_ElementType); },
+      "mdspan: ElementType template parameter must be a complete object type");
   static_assert(!is_array_v<_ElementType>, "mdspan: ElementType template parameter may not be an array type");
   static_assert(!is_abstract_v<_ElementType>, "mdspan: ElementType template parameter may not be an abstract class");
   static_assert(is_same_v<_ElementType, typename _AccessorPolicy::element_type>,

diff  --git a/libcxx/test/std/containers/views/mdspan/mdspan/element_type.verify.cpp b/libcxx/test/std/containers/views/mdspan/mdspan/element_type.verify.cpp
index 9f2730262d953..4fd96cde837ab 100644
--- a/libcxx/test/std/containers/views/mdspan/mdspan/element_type.verify.cpp
+++ b/libcxx/test/std/containers/views/mdspan/mdspan/element_type.verify.cpp
@@ -18,11 +18,57 @@
 
 #include <mdspan>
 
+struct Incomplete;
+
 class AbstractClass {
 public:
   virtual void method() = 0;
 };
 
+struct VoidAccessor {
+  using offset_policy    = VoidAccessor;
+  using element_type     = void;
+  using reference        = void;
+  using data_handle_type = element_type*;
+  reference access(data_handle_type, std::size_t) const;
+};
+
+struct RefAccessor {
+  using offset_policy    = RefAccessor;
+  using element_type     = int&;
+  using reference        = int&;
+  using data_handle_type = int*;
+  reference access(data_handle_type p, std::size_t i) const { return p[i]; }
+};
+
+struct FuncAccessor {
+  using offset_policy    = FuncAccessor;
+  using element_type     = int();
+  using reference        = int (&)();
+  using data_handle_type = int (*)();
+  reference access(data_handle_type, std::size_t) const;
+};
+
+void incomplete_object_type() {
+  // expected-error-re@*:* {{static assertion failed {{.*}}mdspan: ElementType template parameter must be a complete object type}}
+  [[maybe_unused]] std::mdspan<Incomplete, std::dextents<std::size_t, 2>> m;
+}
+
+void void_type() {
+  // expected-error-re@*:* {{static assertion failed {{.*}}mdspan: ElementType template parameter must be a complete object type}}
+  [[maybe_unused]] std::mdspan<void, std::dextents<std::size_t, 2>, std::layout_right, VoidAccessor> m;
+}
+
+void reference_type() {
+  // expected-error-re@*:* {{static assertion failed {{.*}}mdspan: ElementType template parameter must be a complete object type}}
+  [[maybe_unused]] std::mdspan<int&, std::dextents<std::size_t, 2>, std::layout_right, RefAccessor> m;
+}
+
+void function_type() {
+  // expected-error-re@*:* {{static assertion failed {{.*}}mdspan: ElementType template parameter must be a complete object type}}
+  [[maybe_unused]] std::mdspan<int(), std::dextents<std::size_t, 2>, std::layout_right, FuncAccessor> m;
+}
+
 void not_abstract_class() {
   // expected-error-re@*:* {{static assertion failed {{.*}}mdspan: ElementType template parameter may not be an abstract class}}
   [[maybe_unused]] std::mdspan<AbstractClass, std::extents<int>> m;


        


More information about the libcxx-commits mailing list