[mdspan.accessor.aligned.overview]

# 23 Containers library [[containers]](./#containers)

## 23.7 Views [[views]](views#mdspan.accessor.aligned.overview)

### 23.7.3 Multidimensional access [[views.multidim]](views.multidim#mdspan.accessor.aligned.overview)

#### 23.7.3.5 Accessor policy [[mdspan.accessor]](mdspan.accessor#aligned.overview)

#### 23.7.3.5.4 Class template aligned_accessor [[mdspan.accessor.aligned]](mdspan.accessor.aligned#overview)

#### 23.7.3.5.4.1 Overview [mdspan.accessor.aligned.overview]

namespace std {template<class ElementType, size_t ByteAlignment>struct [aligned_accessor](#lib:aligned_accessor "23.7.3.5.4.1 Overview [mdspan.accessor.aligned.overview]") {using offset_policy = default_accessor<ElementType>; using element_type = ElementType; using reference = ElementType&; using data_handle_type = ElementType*; static constexpr size_t byte_alignment = ByteAlignment; constexpr aligned_accessor() noexcept = default; template<class OtherElementType, size_t OtherByteAlignment>constexpr aligned_accessor( aligned_accessor<OtherElementType, OtherByteAlignment>) noexcept; template<class OtherElementType>constexpr explicit aligned_accessor(default_accessor<OtherElementType>) noexcept; template<class OtherElementType>constexpr operator default_accessor<OtherElementType>() const noexcept; constexpr reference access(data_handle_type p, size_t i) const noexcept; constexpr typename offset_policy::data_handle_type offset( data_handle_type p, size_t i) const noexcept; };}

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L24469)

*Mandates*: 

- [(1.1)](#1.1)

byte_alignment is a power of two, and

- [(1.2)](#1.2)

byte_alignment >= alignof(ElementType) is true[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L24476)

aligned_accessor meets the accessor policy requirements[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L24479)

ElementType is required to be a complete object type
that is neither an abstract class type nor an array type[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L24483)

Each specialization of aligned_accessor is
a trivially copyable type that models [semiregular](concepts.object#concept:semiregular "18.6 Object concepts [concepts.object]")[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L24487)

[0, n) is an accessible range
for an object p of type data_handle_type and
an object of type aligned_accessor if and only if

- [(5.1)](#5.1)

[p, p + n) is a valid range, and,

- [(5.2)](#5.2)

if n is greater than zero,
then is_sufficiently_aligned<byte_alignment>(p) is true[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L24499)

[*Example [1](#example-1)*:

The following function compute uses is_sufficiently_aligned to check
whether a given mdspan with default_accessor has
a data handle with sufficient alignment
to be used with aligned_accessor<float, 4 * sizeof(float)>[.](#6.sentence-1)

If so, the function dispatches to
a function compute_using_fourfold_overalignment that requires fourfold over-alignment of arrays,
but can therefore use hardware-specific instructions,
such as four-wide SIMD (Single Instruction Multiple Data) instructions[.](#6.sentence-2)

Otherwise, compute dispatches to a
possibly less optimized function compute_without_requiring_overalignment that has no over-alignment requirement[.](#6.sentence-3)

void compute_using_fourfold_overalignment( std::mdspan<float, std::dims<1>, std::layout_right,
 std::aligned_accessor<float, 4 * alignof(float)>> x);

void compute_without_requiring_overalignment( std::mdspan<float, std::dims<1>, std::layout_right> x);

void compute(std::mdspan<float, std::dims<1>> x) {constexpr auto byte_alignment = 4 * sizeof(float); auto accessor = std::aligned_accessor<float, byte_alignment>{}; auto x_handle = x.data_handle(); if (std::is_sufficiently_aligned<byte_alignment>(x_handle)) { compute_using_fourfold_overalignment(std::mdspan{x_handle, x.mapping(), accessor}); } else { compute_without_requiring_overalignment(x); }} â *end example*]
