Namespaces
Variants

fill(std::mdspan)

From cppreference.com
 
 
 
 
Defined in header <mdspan>
template< class Dst, class T = Dst::value_type >
constexpr void fill( const Dst& dst, const T& value );
(1) (since C++29)
template< class ExecutionPolicy, class Dst, class T = Dst::value_type >
void fill( ExecutionPolicy&& policy, const Dst& dst, const T& value );
(2) (since C++29)

Assigns value to every element of the mdspan dst.

1) Assigns value to each element of dst.
This overload participates in overload resolution only if all following values are true:
  • Dst is a specialization of std::mdspan
  • std::is_assignable_v<typename Dst::reference, const T&>
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

dst - the mdspan to modify
value - the value to assign to the elements
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

Implementations are expected to optimize special cases; e.g., filling an mdspan with a contiguous layout mapping that uses std::default_accessor can be implemented as a single bulk write.

If dst is not unique, the same element can be assigned more than once.

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

Example

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

int main()
{
    std::vector data(6, 0);
    std::mdspan m{data.data(), 2, 3};

    std::fill(m, 42);

    for (std::size_t i = 0; i < m.extent(0); ++i)
    {
        for (std::size_t j = 0; j < m.extent(1); ++j)
            std::print("{} ", m[i, j]);
        std::println();
    }
}

Output:

42 42 42
42 42 42

See also

copy-assigns the given value to every element in a range
(function template & algorithm function object)[edit]
(C++29)
copies each element of source mdspan to the destination mdspan
(function template) [edit]