Namespaces
Variants

copy(std::mdspan)

From cppreference.com
 
 
 
 
Defined in header <mdspan>
template< class Src, class Dst >
constexpr void copy( const Src& src, const Dst& dst );
(1) (since C++29)
template< class ExecutionPolicy, class Src, class Dst >
void copy( ExecutionPolicy&& policy, const Src& src, const Dst& dst );
(2) (since C++29)

Copies the elements of the source mdspan src into the destination mdspan dst. The source and destination views may have different layouts, accessors, and element types.

1) Assigns each element of src to the corresponding element of dst.
This overload participates in overload resolution only if all following values are true:
  • Src and Dst are specializations of std::mdspan
  • std::is_assignable_v<typename Dst::reference, typename Src::reference>
  • std::is_constructible_v<typename Src::extents_type, typename Dst::extents_type>
These constraints effectively verify that the ranks of the extents are the same and that each extent is either static and the same, or std::dynamic_extent.
If src.extents() == dst.extents() is false:
If dst.is_unique() is false, or there are a multidimensional index i in src.extents() and a multidimensional index j in dst.extents() such that src[i] and dst[j] refer to the same element, the behavior is undefined.
2) Same as (1), but the order in which the elements are assigned is determined by policy.
This overload participates in overload resolution only if the value of the following expression is true:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(until C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(since C++20)

Parameters

src - source mdspan
dst - destination mdspan
policy - the execution policy to use

Exceptions

2) During the execution process:
  • If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
  • If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).

Notes

Unlike the iterator-based std::copy, the extents of both views are known, so implementations can (and hardened implementations do) check that src and dst have the same extents, which prevents past-the-end writes.

Implementations are expected to optimize special cases; e.g., copying between two mdspans that have the same layout mapping type that is contiguous and that both use std::default_accessor can be implemented as a single std::memcpy.

Copying between mdspans of different sizes is intentionally not supported; use std::submdspan to select the desired subregions first.

These overloads return void, as there is no past-the-end iterator for an mdspan that they could reasonably return.

The C++26 linear algebra library provides std::linalg::copy, which is limited to mdspans of rank at most 2 and grants implementations the freedom to create arbitrarily many intermediate objects of the value type. A using-declaration for std::linalg::copy can make unqualified calls to copy with mdspan arguments ambiguous, because these std::copy overloads are also found by argument-dependent lookup.

Feature-test macro Value Std Feature
__cpp_lib_mdspan_copy 202606L (C++29) std::copy and std::fill for std::mdspan

Example

The following code copies a row-major (std::layout_right) 2×3 mdspan into a column-major (std::layout_left) one, which only changes the layout of the underlying memory, not the values of the elements.

#include <mdspan>
#include <print>
#include <ranges>
#include <vector>

void print_2D(auto rem, const auto& md)
{
    std::println("{}", rem);
    for (auto [i, j] : std::views::cartesian_product(
            std::views::indices(md.extent(0)),
            std::views::indices(md.extent(1))
        ))
        std::print("{}{}", md[i, j], j + 1 == md.extent(1) ? '\n' : '\t');
}

int main()
{
    std::vector in{0, 1, 2, 3, 4, 5};
    std::vector out{6, 7, 8, 9, 10, 11};

    std::mdspan src{in.data(), 2, 3}; // row-major 2×3 view of “in”
    std::mdspan<int, std::dextents<std::size_t, 2>, std::layout_left>
        dst{out.data(), 2, 3};        // column-major 2×3 view of “out”

    print_2D("src:", src);
    print_2D("dst:", dst);

    std::copy(src, dst);

    print_2D("after copy, dst:", dst);
    std::println("raw “out”: {:n:}", out);
}

Output:

src:
0	1	2
3	4	5
dst:
6	8	10
7	9	11
after copy, dst:
0	1	2
3	4	5
raw “out”: 0, 3, 1, 4, 2, 5

See also

copies a range of elements to a new location
(function template & algorithm function object)[edit]
(C++29)
copy-assigns the given value to each element in an mdspan
(function template) [edit]
(C++26)
returns a view of a subset of an existing mdspan
(function template) [edit]