[libcxx-commits] [libcxx] [libc++] Implement P2897R7 aligned_accessor: An mdspan accessor expressing pointer over-alignment (PR #122603)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 13 02:27:15 PST 2025


================
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// constexpr reference access(data_handle_type p, size_t i) const noexcept;
+//
+// Effects: Equivalent to: return assume_aligned<byte_alignment>(p)[i];
+
+#include <mdspan>
+#include <cassert>
+#include <cstdint>
+#include <concepts>
+#include <type_traits>
+
+#include "test_macros.h"
+
+#include "../MinimalElementType.h"
+
+template <class T, size_t N>
+constexpr void test_access() {
+  ElementPool<std::remove_const_t<T>, 10 + N> data;
+  T* ptr = data.get_ptr();
+  std::aligned_accessor<T, N> acc;
+  for (size_t i = 0; i < 10 + N; i++) {
+    if (reinterpret_cast<std::uintptr_t>(ptr + i) % N == 0) {
+      std::same_as<typename std::aligned_accessor<T, N>::reference> decltype(auto) x = acc.access(ptr, i);
----------------
frederick-vs-ja wrote:

The failures (also in `offset.pass.cpp`) seemed because of that while `ptr + i` was properly aligned, `ptr` was not, which violated the preconditions of `assume_aligned`.

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


More information about the libcxx-commits mailing list