[libcxx-commits] [libcxx] [libc++][mdspan] P3383R3: mdspan.at() (PR #175213)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 16 10:13:19 PST 2026


https://github.com/eiytoq updated https://github.com/llvm/llvm-project/pull/175213

>From 31553a42c79821b66b62cf06709cc1b69d3b48c3 Mon Sep 17 00:00:00 2001
From: eiytoq <eiytoq at outlook.com>
Date: Sat, 10 Jan 2026 01:34:58 +0800
Subject: [PATCH] [libcxx] Implement P3383R3 (mdspan::at)

This patch implements P3383R3 which adds checked access (at) to mdspan.

Co-authored-by: Nikolas Klauser <nikolasklauser at berlin.de>
---
 libcxx/docs/FeatureTestMacroTable.rst         |   2 +-
 libcxx/docs/ReleaseNotes/23.rst               |   2 +
 libcxx/docs/Status/Cxx2cPapers.csv            |   2 +-
 libcxx/include/__mdspan/mdspan.h              |  36 ++
 libcxx/include/mdspan                         |   7 +
 libcxx/include/version                        |   4 +-
 .../views/mdspan/nodiscard.verify.cpp         |   8 +
 .../views/mdspan/CustomTestLayouts.h          |   1 +
 .../views/mdspan/mdspan/at.pass.cpp           | 335 ++++++++++++++++++
 .../mdspan.version.compile.pass.cpp           |   4 +-
 .../version.version.compile.pass.cpp          |   4 +-
 .../generate_feature_test_macro_components.py |   1 +
 12 files changed, 398 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/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index 73f5984768592..9929acdfd05fd 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -38,6 +38,8 @@ What's New in Libc++ 23.0.0?
 Implemented Papers
 ------------------
 
+- P3383R3: ``mdspan.at()`` (`Github <https://llvm.org/PR175213>`__)
+
 Improvements and New Features
 -----------------------------
 
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 449baea43f2d7..b14116a8a1a70 100644
--- a/libcxx/include/__mdspan/mdspan.h
+++ b/libcxx/include/__mdspan/mdspan.h
@@ -40,6 +40,7 @@
 #include <__utility/integer_sequence.h>
 #include <array>
 #include <span>
+#include <stdexcept>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -213,6 +214,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 (*this)[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
@@ -269,6 +301,10 @@ class mdspan {
 
   template <class, class, class, class>
   friend class mdspan;
+
+#  if _LIBCPP_STD_VER >= 26
+  _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 d47f0cb6cd41c..2f6b451317e98 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..f17ef6ee870d4 100644
--- a/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp
@@ -16,6 +16,8 @@
 #include <mdspan>
 #include <span>
 
+#include "test_macros.h"
+
 void test() {
   // mdspan<>
 
@@ -28,6 +30,12 @@ void test() {
   std::span sp{arr};
   mdsp[sp]; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 
+#if TEST_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}}
   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..78670b03dc8d0 100644
--- a/libcxx/test/std/containers/views/mdspan/CustomTestLayouts.h
+++ b/libcxx/test/std/containers/views/mdspan/CustomTestLayouts.h
@@ -335,4 +335,5 @@ class always_convertible_layout::mapping {
   index_type offset_{};
   index_type scaling_{};
 };
+
 #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..493f10525865c
--- /dev/null
+++ b/libcxx/test/std/containers/views/mdspan/mdspan/at.pass.cpp
@@ -0,0 +1,335 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+
+// <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
+#include <vector>
+
+#include "test_macros.h"
+
+#include "../ConvertibleToIntegral.h"
+#include "../CustomTestLayouts.h"
+
+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_{};
+};
+
+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) {
+    std::same_as<typename MDS::reference> decltype(auto) 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...)
+    std::same_as<typename MDS::reference> decltype(auto) ptr_at = mds.at(args...);
+    assert(&ptr_at == &ptr_accessor);
+
+    //  mdspan.at(array)
+    std::same_as<typename MDS::reference> decltype(auto) ptr_arr = mds.at(args_arr);
+    assert(&ptr_arr == &ptr_accessor);
+
+    // mdspan.at(span)
+    std::same_as<typename MDS::reference> decltype(auto) 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() {
+  test_iteration(construct_mapping(Layout{}, std::extents<int>{}));
+  test_iteration(construct_mapping(Layout{}, std::dextents<unsigned, 1>{1}));
+  test_iteration(construct_mapping(Layout{}, std::dextents<unsigned, 1>{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::dextents<signed char, 4>{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::dextents<int, 1>{1})}, 0));
+  static_assert(!check_at_constraints(std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{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::dextents<int, 1>{1})}, IntType{0}));
+  static_assert(
+      !check_at_constraints(std::mdspan{data, construct_mapping(Layout{}, std::dextents<unsigned, 1>{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::dextents<unsigned char, 1>{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::dextents<unsigned char, 2>{1, 1})}, 0, 0uz));
+  static_assert(check_at_constraints(
+      std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 2>{1, 1})}, 0u, IntType{0}));
+
+  constexpr bool t = true;
+  constexpr bool o = false;
+  static_assert(!check_at_constraints(
+      std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 2>{1, 1})}, 0uz, IntConfig<o, o, t, t>{0}));
+  static_assert(check_at_constraints(
+      std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 2>{1, 1})}, 0uz, IntConfig<o, t, t, t>{0}));
+  static_assert(check_at_constraints(
+      std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 2>{1, 1})}, 0uz, IntConfig<o, t, o, t>{0}));
+  static_assert(!check_at_constraints(
+      std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 2>{1, 1})}, 0uz, IntConfig<t, o, o, t>{0}));
+  static_assert(check_at_constraints(
+      std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 2>{1, 1})}, 0uz, IntConfig<t, o, t, o>{0}));
+  static_assert(check_at_constraints(
+      std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 2>{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::dextents<int, 1>{1})},
+                                        std::array{IntConfig<o, o, t, t>{0}}));
+    static_assert(!check_at_constraints(std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})},
+                                        std::array{IntConfig<o, t, t, t>{0}}));
+    static_assert(!check_at_constraints(std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})},
+                                        std::array{IntConfig<t, o, o, t>{0}}));
+    static_assert(!check_at_constraints(std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})},
+                                        std::array{IntConfig<t, t, o, t>{0}}));
+    static_assert(check_at_constraints(std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})},
+                                       std::array{IntConfig<t, o, t, o>{0}}));
+    static_assert(check_at_constraints(std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})},
+                                       std::array{IntConfig<t, t, t, t>{0}}));
+
+    {
+      std::array idx{IntConfig<o, o, t, t>{0}};
+      assert(!check_at_constraints(
+          std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})}, std::span{idx}));
+    }
+    {
+      std::array idx{IntConfig<o, t, t, t>{0}};
+      assert(!check_at_constraints(
+          std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})}, std::span{idx}));
+    }
+    {
+      std::array idx{IntConfig<t, o, o, t>{0}};
+      assert(!check_at_constraints(
+          std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})}, std::span{idx}));
+    }
+    {
+      std::array idx{IntConfig<t, t, o, t>{0}};
+      assert(!check_at_constraints(
+          std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})}, std::span{idx}));
+    }
+    {
+      std::array idx{IntConfig<t, o, t, o>{0}};
+      assert(check_at_constraints(
+          std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{1})}, std::span{idx}));
+    }
+    {
+      std::array idx{IntConfig<t, t, t, t>{0}};
+      assert(check_at_constraints(
+          std::mdspan{data, construct_mapping(Layout{}, std::dextents<int, 1>{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};
+  const MDS m{data, std::dextents<int, 1>{10}};
+
+  SpyIndex idx_val(3);
+
+  {
+    assert(&m.at(idx_val) == &data[3]);
+  }
+
+  {
+    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;
+}
+
+static 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;
+}
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 aacccc0451693..1307cb660c2c8 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 a847042bf47c0..abdf01842fd04 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"],
         },



More information about the libcxx-commits mailing list