copy(std::mdspan)
| 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.
src to the corresponding element of dst.true:
SrcandDstare specializations of std::mdspanstd::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.
src.extents() == dst.extents() is false:
- If the implementation is hardened, a contract violation occurs.
- If the implementation is not hardened, the behavior is undefined.
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.policy.true:
|
|
(until C++20) |
|
|
(since C++20) |
Parameters
| src | - | source mdspan
|
| dst | - | destination mdspan
|
| policy | - | the execution policy to use |
Exceptions
- 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
(C++11) |
copies a range of elements to a new location (function template & algorithm function object) |
(C++20)(C++20) |
|
(C++29) |
copy-assigns the given value to each element in an mdspan (function template) |
(C++26) |
returns a view of a subset of an existing mdspan (function template) |