[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:12 PDT 2026
https://github.com/eiytoq created https://github.com/llvm/llvm-project/pull/191703
Fixes: #191688
>From bde39380eff10db85da385a06341bc156e679136 Mon Sep 17 00:00:00 2001
From: eiytoq <eiytoq at outlook.com>
Date: Sun, 12 Apr 2026 20:14:41 +0800
Subject: [PATCH] fix
---
libcxx/include/__mdspan/mdspan.h | 3 +++
.../views/mdspan/mdspan/element_type.verify.cpp | 13 +++++++++++++
2 files changed, 16 insertions(+)
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;
More information about the libcxx-commits
mailing list