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

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Apr 12 05:23:46 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: eiytoq (eiytoq)

<details>
<summary>Changes</summary>

Fixes: #<!-- -->191688


---
Full diff: https://github.com/llvm/llvm-project/pull/191703.diff


2 Files Affected:

- (modified) libcxx/include/__mdspan/mdspan.h (+3) 
- (modified) libcxx/test/std/containers/views/mdspan/mdspan/element_type.verify.cpp (+13) 


``````````diff
diff --git a/libcxx/include/__mdspan/mdspan.h b/libcxx/include/__mdspan/mdspan.h
index 449baea43f2d7..838bfac1ea8c3 100644
--- a/libcxx/include/__mdspan/mdspan.h
+++ b/libcxx/include/__mdspan/mdspan.h
@@ -66,6 +66,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) > 0; },
+      "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..13d8bc9111f75 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
@@ -23,6 +23,19 @@ class AbstractClass {
   virtual void method() = 0;
 };
 
+struct BadAccessor {
+  using offset_policy = BadAccessor;
+  using element_type = void;
+  using reference    = void;
+  using data_handle_type = element_type*;
+  reference access(data_handle_type, size_t) const;
+};
+
+int incomplete_type() {
+  // expected-error-re@*:* {{static assertion failed {{.*}}mdspan: ElementType template parameter must be a complete object type}}
+  [[maybe_unused]] std::mdspan<void, std::dims<2>, std::layout_right, BadAccessor> 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;

``````````

</details>


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


More information about the libcxx-commits mailing list