[libcxx-commits] [libcxx] [libc++][mdspan] P3383R3: mdspan.at() (PR #175213)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jan 9 21:26:09 PST 2026
================
@@ -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);
----------------
H-G-Hristov wrote:
Can we test the return type? The usual common pattern is:
```c++
std::same_as<ReturnType> decltype(auto) returnValue = function();
```
https://github.com/llvm/llvm-project/pull/175213
More information about the libcxx-commits
mailing list