[ Web Proxy ]
URL:
Viewing: https://en.cppreference.com/cpp/container/mdspan/operator_at [Back]  [Original]

std::mdspan<T,Extents,LayoutPolicy,AccessorPolicy>::operator[] - cppreference.com
cppreference.com
Namespaces
Variants

std::mdspan<T,Extents,LayoutPolicy,AccessorPolicy>::operator[]

From cppreference.com
 
 
 
 
template< class... OtherIndexTypes >
constexpr reference operator[]( OtherIndexTypes... indices ) const;
(1) (since C++23)
template< class OtherIndexType >
constexpr reference
    operator[]( std::span<OtherIndexType, rank()> indices ) const;
(2) (since C++23)
template< class OtherIndexType >
constexpr reference
    operator[]( const std::array<OtherIndexType, rank()>& indices ) const;
(3) (since C++23)

Returns a reference to the indicesth element of the mdspan.

1) Equivalent to return acc_ .access(ptr_, map_ (static_cast<index_type>(std::move(indices))...));.
This overload participates in overload resolution only if all following values are true:
  • (std::is_convertible_v<OtherIndexTypes, index_type> && ...)
  • (std::is_nothrow_constructible_v<index_type, OtherIndexTypes> && ...)
  • sizeof...(OtherIndexTypes) == rank()

If extents_type::index-cast (std::move(indices)) is not a multidimensional index in extents(), the behavior is undefined.

(until C++26)

If extents_type::index-cast (std::move(indices)) is not a multidimensional index in extents():

(since C++26)
2,3) Let P be a parameter pack such that

std::is_same_v<std::make_index_sequence<rank()>
               std::index_sequence<P...>>

is true, equivalent to return operator[](extents_type::index-cast (std::as_const(indices[P]))...);.
This overload participates in overload resolution only if all following values are true:
  • std::is_convertible_v<const OtherIndexType&, index_type>
  • std::is_nothrow_constructible_v<index_type, const OtherIndexType&>

Parameters

indices - the indices of the element to access

Return value

A reference to the element.

Example

#include <array>
#include <mdspan>
#include <span>

int main()
{
    static constexpr std::array data // row-major 2 x 3 matrix
    {
        10, 11, 12, // row 0
        20, 21, 22  // row 1
    };

    constexpr std::mdspan m{data.data(), 2, 3}; // 2D mdspan (2 rows, 3 columns)
    static_assert(m[1, 2] == 22, "Overload (1)");

    // Coordinates are stored in a fixed-size container
    constexpr std::array<std::size_t, 2> arr_indices{0, 1};
    static_assert(m[arr_indices] == 11, "Overload (3)");
    static_assert(m[arr_indices] == m[0, 1]);

    // Useful in dynamic contexts or interfaces that get multi-dimensional coordinates
    static constexpr std::array<std::size_t, 2> raw_indices{1, 0};
    constexpr std::span<const std::size_t, 2> span_indices{raw_indices};
    static_assert(m[span_indices] == 20, "Overload (2)");
    static_assert(m[span_indices] == m[1, 0]);
}

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3974 C++23 overloads (2,3) did not apply extents_type::index-cast applies

See also

(C++26)
accesses an element at the specified multidimensional index with bounds checking
(public member function) [edit]

Web Proxy Viewer  |  New URL  |  Original Page