[libcxx-commits] [libcxx] [libc++][mdspan] P3383R3: mdspan.at() (PR #175213)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jan 9 13:27:25 PST 2026
https://github.com/eiytoq updated https://github.com/llvm/llvm-project/pull/175213
>From 481e5591cf94bf462087516ef74dd8cf7ed735ca Mon Sep 17 00:00:00 2001
From: eiytoq <eiytoq at outlook.com>
Date: Sat, 10 Jan 2026 01:34:58 +0800
Subject: [PATCH 1/3] Implement P3383R3
---
libcxx/docs/FeatureTestMacroTable.rst | 2 +-
libcxx/docs/Status/Cxx2cPapers.csv | 2 +-
libcxx/include/__mdspan/mdspan.h | 38 +++
libcxx/include/mdspan | 7 +
libcxx/include/version | 4 +-
.../views/mdspan/nodiscard.verify.cpp | 4 +
.../views/mdspan/CustomTestLayouts.h | 76 +++++
.../views/mdspan/mdspan/at.pass.cpp | 262 ++++++++++++++++++
.../mdspan.version.compile.pass.cpp | 4 +-
.../version.version.compile.pass.cpp | 4 +-
.../generate_feature_test_macro_components.py | 1 +
11 files changed, 396 insertions(+), 8 deletions(-)
create mode 100644 libcxx/test/std/containers/views/mdspan/mdspan/at.pass.cpp
diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index 32911d0f64449..abe478b6a8d2b 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -484,7 +484,7 @@ Status
---------------------------------------------------------- -----------------
``__cpp_lib_linalg`` *unimplemented*
---------------------------------------------------------- -----------------
- ``__cpp_lib_mdspan`` ``202406L``
+ ``__cpp_lib_mdspan`` ``202506L``
---------------------------------------------------------- -----------------
``__cpp_lib_not_fn`` ``202306L``
---------------------------------------------------------- -----------------
diff --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index 29642fc53cac6..93843450b5cba 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -134,7 +134,7 @@
"`P3480R6 <https://wg21.link/P3480R6>`__","``std::simd`` is a range","2025-06 (Sofia)","","","`#148144 <https://github.com/llvm/llvm-project/issues/148144>`__",""
"`P2664R11 <https://wg21.link/P2664R11>`__","Extend ``std::simd`` with permutation API","2025-06 (Sofia)","","","`#148145 <https://github.com/llvm/llvm-project/issues/148145>`__",""
"`P3691R1 <https://wg21.link/P3691R1>`__","Reconsider naming of the namespace for ``std::simd``","2025-06 (Sofia)","","","`#148148 <https://github.com/llvm/llvm-project/issues/148148>`__",""
-"`P3383R3 <https://wg21.link/P3383R3>`__","``mdspan.at()``","2025-06 (Sofia)","","","`#148149 <https://github.com/llvm/llvm-project/issues/148149>`__",""
+"`P3383R3 <https://wg21.link/P3383R3>`__","``mdspan.at()``","2025-06 (Sofia)","|Complete|","22","`#148149 <https://github.com/llvm/llvm-project/issues/148149>`__",""
"`P2927R3 <https://wg21.link/P2927R3>`__","Inspecting ``exception_ptr``","2025-06 (Sofia)","","","`#148150 <https://github.com/llvm/llvm-project/issues/148150>`__",""
"`P3748R0 <https://wg21.link/P3748R0>`__","Inspecting ``exception_ptr`` should be constexpr","2025-06 (Sofia)","","","`#148151 <https://github.com/llvm/llvm-project/issues/148151>`__",""
"`P2830R10 <https://wg21.link/P2830R10>`__","Standardized Constexpr Type Ordering","2025-06 (Sofia)","","","`#148152 <https://github.com/llvm/llvm-project/issues/148152>`__",""
diff --git a/libcxx/include/__mdspan/mdspan.h b/libcxx/include/__mdspan/mdspan.h
index 9d3d35cd558a1..916197a3827bd 100644
--- a/libcxx/include/__mdspan/mdspan.h
+++ b/libcxx/include/__mdspan/mdspan.h
@@ -40,6 +40,9 @@
#include <__utility/integer_sequence.h>
#include <array>
#include <span>
+#if _LIBCPP_STD_VER >= 26
+# include <stdexcept>
+#endif
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -214,6 +217,37 @@ class mdspan {
}(make_index_sequence<rank()>()));
}
+# if _LIBCPP_STD_VER >= 26
+ template <class... _OtherIndexTypes>
+ requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&
+ (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&
+ (sizeof...(_OtherIndexTypes) == rank()))
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference at(_OtherIndexTypes... __indices) const {
+ if (!__mdspan_detail::__is_multidimensional_index_in(__map_.extents(), __indices...)) {
+ __throw_out_of_range();
+ }
+ return operator[](static_cast<index_type>(std::move(__indices))...);
+ }
+
+ template <class _OtherIndexType>
+ requires(is_convertible_v<const _OtherIndexType&, index_type> &&
+ is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference at(const array<_OtherIndexType, rank()>& __indices) const {
+ return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) -> reference {
+ return at(static_cast<index_type>(__indices[_Idxs])...);
+ }(make_index_sequence<rank()>());
+ }
+
+ template <class _OtherIndexType>
+ requires(is_convertible_v<const _OtherIndexType&, index_type> &&
+ is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference at(span<_OtherIndexType, rank()> __indices) const {
+ return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) -> reference {
+ return at(static_cast<index_type>(__indices[_Idxs])...);
+ }(make_index_sequence<rank()>());
+ }
+# endif
+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept {
// Could leave this as only checked in debug mode: semantically size() is never
// guaranteed to be related to any accessible range
@@ -270,6 +304,10 @@ class mdspan {
template <class, class, class, class>
friend class mdspan;
+
+# if _LIBCPP_STD_VER >= 26
+ [[noreturn]] _LIBCPP_HIDE_FROM_ABI void __throw_out_of_range() const { std::__throw_out_of_range("mdspan"); }
+# endif
};
# if _LIBCPP_STD_VER >= 26
diff --git a/libcxx/include/mdspan b/libcxx/include/mdspan
index 32468a128dc9a..f85353cc9d758 100644
--- a/libcxx/include/mdspan
+++ b/libcxx/include/mdspan
@@ -364,6 +364,13 @@ namespace std {
template<class OtherIndexType>
constexpr reference operator[](const array<OtherIndexType, rank()>& indices) const;
+ template<class... OtherIndexTypes>
+ constexpr reference at(OtherIndexTypes... indices) const;
+ template<class OtherIndexType>
+ constexpr reference at(span<OtherIndexType, rank()> indices) const;
+ template<class OtherIndexType>
+ constexpr reference at(const array<OtherIndexType, rank()>& indices) const;
+
constexpr size_type size() const noexcept;
[[nodiscard]] constexpr bool empty() const noexcept;
diff --git a/libcxx/include/version b/libcxx/include/version
index 1d2422e64ba0f..1049ff7188463 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -174,7 +174,7 @@ __cpp_lib_make_unique 201304L <memory>
__cpp_lib_map_try_emplace 201411L <map>
__cpp_lib_math_constants 201907L <numbers>
__cpp_lib_math_special_functions 201603L <cmath>
-__cpp_lib_mdspan 202406L <mdspan>
+__cpp_lib_mdspan 202506L <mdspan>
202207L // C++23
__cpp_lib_memory_resource 201603L <memory_resource>
__cpp_lib_move_iterator_concept 202207L <iterator>
@@ -593,7 +593,7 @@ __cpp_lib_void_t 201411L <type_traits>
# endif
// # define __cpp_lib_linalg 202311L
# undef __cpp_lib_mdspan
-# define __cpp_lib_mdspan 202406L
+# define __cpp_lib_mdspan 202506L
# undef __cpp_lib_not_fn
# define __cpp_lib_not_fn 202306L
# undef __cpp_lib_optional
diff --git a/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp b/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp
index d248656ec0ae5..b132c4a6da09b 100644
--- a/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp
@@ -28,6 +28,10 @@ void test() {
std::span sp{arr};
mdsp[sp]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ mdsp.at(0, 1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ mdsp.at(arr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ mdsp.at(sp); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
mdsp.rank(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
mdsp.rank_dynamic(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
mdsp.static_extent(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
diff --git a/libcxx/test/std/containers/views/mdspan/CustomTestLayouts.h b/libcxx/test/std/containers/views/mdspan/CustomTestLayouts.h
index 588a5e9774a55..4b33f11dc0086 100644
--- a/libcxx/test/std/containers/views/mdspan/CustomTestLayouts.h
+++ b/libcxx/test/std/containers/views/mdspan/CustomTestLayouts.h
@@ -335,4 +335,80 @@ class always_convertible_layout::mapping {
index_type offset_{};
index_type scaling_{};
};
+
+struct SpyIndex {
+ int val;
+ constexpr SpyIndex(int v) : val(v) {}
+ constexpr operator int() const noexcept { return val; }
+};
+
+class strict_cast_layout {
+public:
+ template <class Extents>
+ class mapping;
+};
+
+template <class Extents>
+class strict_cast_layout::mapping {
+public:
+ using extents_type = Extents;
+ using index_type = typename extents_type::index_type;
+ using size_type = typename extents_type::size_type;
+ using rank_type = typename extents_type::rank_type;
+ using layout_type = strict_cast_layout;
+
+ constexpr mapping() noexcept = default;
+ constexpr mapping(const mapping&) noexcept = default;
+ constexpr mapping(const extents_type& ext) noexcept : extents_(ext) {}
+
+ template <class OtherExtents>
+ constexpr mapping(const mapping<OtherExtents>& other) noexcept : extents_(other.extents()) {}
+
+ constexpr mapping& operator=(const mapping&) noexcept = default;
+
+ constexpr const extents_type& extents() const noexcept { return extents_; }
+
+ constexpr index_type required_span_size() const noexcept {
+ index_type size = 1;
+ for (rank_type r = 0; r < extents_type::rank(); r++)
+ size *= extents_.extent(r);
+ return size;
+ }
+
+ template <std::integral... Indices>
+ requires(sizeof...(Indices) == extents_type::rank())
+ constexpr index_type operator()(Indices... idx) const noexcept {
+ return [&]<size_t... _Is>(std::index_sequence<_Is...>) {
+ index_type res = 0;
+ std::array<index_type, sizeof...(Indices)> idx_arr{static_cast<index_type>(idx)...};
+ ((res = res * extents_.extent(_Is) + idx_arr[_Is]), ...);
+ return res;
+ }(std::make_index_sequence<sizeof...(Indices)>{});
+ }
+
+ constexpr index_type operator()(SpyIndex) const noexcept = delete;
+
+ static constexpr bool is_always_unique() noexcept { return true; }
+ static constexpr bool is_always_exhaustive() noexcept { return true; }
+ static constexpr bool is_always_strided() noexcept { return true; }
+
+ static constexpr bool is_unique() noexcept { return true; }
+ static constexpr bool is_exhaustive() noexcept { return true; }
+ static constexpr bool is_strided() noexcept { return true; }
+
+ constexpr index_type stride(rank_type r) const noexcept {
+ index_type s = 1;
+ for (rank_type i = r + 1; i < extents_type::rank(); i++)
+ s *= extents_.extent(i);
+ return s;
+ }
+
+ friend constexpr bool operator==(const mapping& lhs, const mapping& rhs) noexcept {
+ return lhs.extents_ == rhs.extents_;
+ }
+
+private:
+ extents_type extents_{};
+};
+
#endif // TEST_STD_CONTAINERS_VIEWS_MDSPAN_CUSTOM_TEST_LAYOUTS_H
diff --git a/libcxx/test/std/containers/views/mdspan/mdspan/at.pass.cpp b/libcxx/test/std/containers/views/mdspan/mdspan/at.pass.cpp
new file mode 100644
index 0000000000000..8fdb721e5ebe4
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/mdspan/at.pass.cpp
@@ -0,0 +1,262 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <mdspan>
+
+// template<class... OtherIndexTypes>
+// constexpr reference at(OtherIndexTypes... indices) const;
+//
+// template<class OtherIndexType>
+// constexpr reference at(span<OtherIndexType, rank()> indices) const;
+//
+// template<class OtherIndexType>
+// constexpr reference at(const array<OtherIndexType, rank()>& indices) const;
+//
+// Constraints:
+// * sizeof...(OtherIndexTypes) == extents_type::rank() is true,
+// * (is_convertible_v<OtherIndexTypes, index_type> && ...) is true,
+// * (is_nothrow_constructible_v<index_type, OtherIndexTypes> && ...) is true.
+//
+// Throws:
+// * std::out_of_range if extents_type::index-cast(indices) is not a multidimensional index in extents_.
+
+#include <array>
+#include <cassert>
+#include <mdspan>
+#include <span> // dynamic_extent
+#ifndef TEST_HAS_NO_EXCEPTIONS
+# include <vector>
+#endif
+
+#include "test_macros.h"
+
+#include "../ConvertibleToIntegral.h"
+#include "../CustomTestLayouts.h"
+
+template <class MDS, class... Indices>
+concept at_constraints = requires(MDS m, Indices... idxs) {
+ { m.at(idxs...) } -> std::same_as<typename MDS::reference>;
+};
+
+template <class MDS, class... Indices>
+ requires(at_constraints<MDS, Indices...>)
+constexpr bool check_at_constraints(MDS m, Indices... idxs) {
+ TEST_IGNORE_NODISCARD m.at(idxs...);
+ return true;
+}
+
+template <class MDS, class... Indices>
+constexpr bool check_at_constraints(MDS, Indices...) {
+ return false;
+}
+
+template <class MDS, class... Args>
+constexpr void iterate(MDS mds, Args... args) {
+ constexpr int r = static_cast<int>(MDS::extents_type::rank()) - 1 - static_cast<int>(sizeof...(Args));
+
+ if constexpr (r == -1) {
+ [[maybe_unused]] int* ptr_accessor = &(mds.accessor().access(mds.data_handle(), mds.mapping()(args...)));
+ std::array<typename MDS::index_type, MDS::rank()> args_arr{static_cast<typename MDS::index_type>(args)...};
+
+ // mdspan.at(indices...)
+ [[maybe_unused]] typename MDS::element_type* ptr_at = &mds.at(args...);
+ assert(ptr_at == ptr_accessor);
+
+ // mdspan.at(array)
+ [[maybe_unused]] typename MDS::element_type* ptr_arr = &mds.at(args_arr);
+ assert(&mds.at(args_arr) == ptr_accessor);
+
+ // mdspan.at(span)
+ [[maybe_unused]] typename MDS::element_type* ptr_span = &mds.at(std::span(args_arr));
+ assert(ptr_span == ptr_accessor);
+
+ } else {
+ for (typename MDS::index_type i = 0; i < mds.extents().extent(r); i++) {
+ iterate(mds, i, args...);
+ }
+ }
+}
+
+template <class Mapping>
+constexpr void test_iteration(Mapping m) {
+ std::array<int, 1024> data;
+ using MDS = std::mdspan<int, typename Mapping::extents_type, typename Mapping::layout_type>;
+ MDS mds(data.data(), m);
+ iterate(mds);
+}
+
+template <class Layout>
+constexpr void test_layout() {
+ constexpr size_t D = std::dynamic_extent;
+ test_iteration(construct_mapping(Layout(), std::extents<int>()));
+ test_iteration(construct_mapping(Layout(), std::extents<unsigned, D>(1)));
+ test_iteration(construct_mapping(Layout(), std::extents<unsigned, D>(7)));
+ test_iteration(construct_mapping(Layout(), std::extents<unsigned, 7>()));
+ test_iteration(construct_mapping(Layout(), std::extents<unsigned, 7, 8>()));
+ test_iteration(construct_mapping(Layout(), std::extents<signed char, D, D, D, D>(1, 1, 1, 1)));
+
+ int data[1];
+ // Check at constraint for number of arguments
+ static_assert(check_at_constraints(std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), 0));
+ static_assert(!check_at_constraints(std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), 0, 0));
+
+ // Check at constraint for convertibility of arguments to index_type
+ static_assert(
+ check_at_constraints(std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), IntType(0)));
+ static_assert(
+ !check_at_constraints(std::mdspan(data, construct_mapping(Layout(), std::extents<unsigned, D>(1))), IntType(0)));
+
+ // Check at constraint for no-throw-constructibility of index_type from arguments
+ static_assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<unsigned char, D>(1))), IntType(0)));
+
+ // Check that mixed integrals work: note the second one tests that mdspan casts: layout_wrapping_integral does not accept IntType
+ static_assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<unsigned char, D, D>(1, 1))), int(0), size_t(0)));
+ static_assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D, D>(1, 1))), 0uz, IntType(0)));
+
+ constexpr bool t = true;
+ constexpr bool o = false;
+ static_assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D, D>(1, 1))), 0uz, IntConfig<o, o, t, t>(0)));
+ static_assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D, D>(1, 1))), 0uz, IntConfig<o, t, t, t>(0)));
+ static_assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D, D>(1, 1))), 0uz, IntConfig<o, t, o, t>(0)));
+ static_assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D, D>(1, 1))), 0uz, IntConfig<t, o, o, t>(0)));
+ static_assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D, D>(1, 1))), 0uz, IntConfig<t, o, t, o>(0)));
+ static_assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D, D>(1, 1))), 0uz, IntConfig<t, t, t, t>(0)));
+
+ // layout_wrapped wouldn't quite work here the way we wrote the check
+ // IntConfig has configurable conversion properties: convert from const&, convert from non-const, no-throw-ctor from const&, no-throw-ctor from non-const
+ if constexpr (std::is_same_v<Layout, std::layout_left>) {
+ static_assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::array{IntConfig<o, o, t, t>(0)}));
+ static_assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::array{IntConfig<o, t, t, t>(0)}));
+ static_assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::array{IntConfig<t, o, o, t>(0)}));
+ static_assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::array{IntConfig<t, t, o, t>(0)}));
+ static_assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::array{IntConfig<t, o, t, o>(0)}));
+ static_assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::array{IntConfig<t, t, t, t>(0)}));
+
+ {
+ [[maybe_unused]] std::array idx{IntConfig<o, o, t, t>(0)};
+ assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::span(idx)));
+ }
+ {
+ [[maybe_unused]] std::array idx{IntConfig<o, t, t, t>(0)};
+ assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::span(idx)));
+ }
+ {
+ [[maybe_unused]] std::array idx{IntConfig<t, o, o, t>(0)};
+ assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::span(idx)));
+ }
+ {
+ [[maybe_unused]] std::array idx{IntConfig<t, t, o, t>(0)};
+ assert(!check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::span(idx)));
+ }
+ {
+ [[maybe_unused]] std::array idx{IntConfig<t, o, t, o>(0)};
+ assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::span(idx)));
+ }
+ {
+ [[maybe_unused]] std::array idx{IntConfig<t, t, t, t>(0)};
+ assert(check_at_constraints(
+ std::mdspan(data, construct_mapping(Layout(), std::extents<int, D>(1))), std::span(idx)));
+ }
+ }
+}
+
+constexpr bool test() {
+ test_layout<std::layout_left>();
+ test_layout<std::layout_right>();
+ test_layout<layout_wrapping_integral<4>>();
+ return true;
+}
+
+constexpr bool test_index_casting() {
+ using MDS = std::mdspan<int, std::dextents<int, 1>, strict_cast_layout>;
+
+ int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+ MDS m(data, std::dextents<int, 1>(10));
+
+ SpyIndex idx_val(3);
+
+ {
+ assert(&m.at(idx_val) == &data[3]);
+ }
+
+ {
+ [[maybe_unused]] std::array<SpyIndex, 1> indices{idx_val};
+ assert(&m.at(indices) == &data[3]);
+ }
+
+ {
+ std::array<SpyIndex, 1> arr{idx_val};
+ std::span indices(arr);
+ assert(&m.at(indices) == &data[3]);
+ }
+
+ return true;
+}
+
+void test_exceptions() {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+ std::vector<int> data(100);
+ std::mdspan m(data.data(), 10, 10);
+
+ auto check_throw = [&](auto... args) {
+ try {
+ TEST_IGNORE_NODISCARD m.at(args...);
+ assert(false);
+ } catch (const std::out_of_range&) {
+ return; // Expected
+ } catch (...) {
+ assert(false);
+ }
+ };
+
+ assert(m.at(9, 9) == data[99]);
+
+ check_throw(10, 0);
+ check_throw(0, 10);
+ check_throw(-1, 0);
+
+ check_throw(std::array<int, 2>{10, 0});
+
+ std::array<int, 2> invalid_idx{0, 10};
+ check_throw(std::span(invalid_idx));
+#endif
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ test_index_casting();
+ static_assert(test_index_casting());
+
+ test_exceptions();
+
+ return 0;
+}
\ No newline at end of file
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/mdspan.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/mdspan.version.compile.pass.cpp
index fad0e5b9777dd..c1e433f344637 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/mdspan.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/mdspan.version.compile.pass.cpp
@@ -136,8 +136,8 @@
# ifndef __cpp_lib_mdspan
# error "__cpp_lib_mdspan should be defined in c++26"
# endif
-# if __cpp_lib_mdspan != 202406L
-# error "__cpp_lib_mdspan should have the value 202406L in c++26"
+# if __cpp_lib_mdspan != 202506L
+# error "__cpp_lib_mdspan should have the value 202506L in c++26"
# endif
# if !defined(_LIBCPP_VERSION)
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index d54e229a87fcb..73c6c1c6cf9a8 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -7410,8 +7410,8 @@
# ifndef __cpp_lib_mdspan
# error "__cpp_lib_mdspan should be defined in c++26"
# endif
-# if __cpp_lib_mdspan != 202406L
-# error "__cpp_lib_mdspan should have the value 202406L in c++26"
+# if __cpp_lib_mdspan != 202506L
+# error "__cpp_lib_mdspan should have the value 202506L in c++26"
# endif
# if !defined(_LIBCPP_VERSION) || _LIBCPP_AVAILABILITY_HAS_PMR
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 370bc5d26b535..cf89340575dc4 100644
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -950,6 +950,7 @@ def add_version_header(tc):
"values": {
"c++23": 202207,
"c++26": 202406, # P2389R2 dextents Index Type Parameter
+ "c++26": 202506,
},
"headers": ["mdspan"],
},
>From e9d02999619602a000769eea6cc521b708621150 Mon Sep 17 00:00:00 2001
From: eiytoq <eiytoq at outlook.com>
Date: Sat, 10 Jan 2026 02:08:02 +0800
Subject: [PATCH 2/3] Update Release Notes
---
libcxx/docs/ReleaseNotes/22.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/libcxx/docs/ReleaseNotes/22.rst b/libcxx/docs/ReleaseNotes/22.rst
index 3e2b3e6633bb1..d3596a2561ab2 100644
--- a/libcxx/docs/ReleaseNotes/22.rst
+++ b/libcxx/docs/ReleaseNotes/22.rst
@@ -55,6 +55,7 @@ Implemented Papers
- P3567R2: ``flat_meow`` Fixes (`Github <https://llvm.org/PR162022>`__)
- P3836R2: Make ``optional<T&>`` trivially copyable (`Github <https://llvm.org/PR171275>`__)
- P1789R3: Library Support for Expansion Statements (`Github <https://llvm.org/PR167184>`__)
+- P3383R3: ``mdspan.at()`` (`Github <https://llvm.org/PR175213>`__)
Improvements and New Features
-----------------------------
>From 170f2fe1b8e7921b1d24e2de19268eb3061565b4 Mon Sep 17 00:00:00 2001
From: eiytoq <eiytoq at outlook.com>
Date: Sat, 10 Jan 2026 04:18:59 +0800
Subject: [PATCH 3/3] Guard mdspan::at tests
---
libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp b/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp
index b132c4a6da09b..2aaa532a2b40e 100644
--- a/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp
@@ -28,9 +28,11 @@ void test() {
std::span sp{arr};
mdsp[sp]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#if _LIBCPP_STD_VER >= 26
mdsp.at(0, 1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
mdsp.at(arr); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
mdsp.at(sp); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
mdsp.rank(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
mdsp.rank_dynamic(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
More information about the libcxx-commits
mailing list