std::ranges::uninitialized_fill_n

Header: <memory>

Copies value to an uninitialized memory area first+[0,count) as if by return ranges::uninitialized_fill(std::counted_iterator(first, count),std::default_sentinel, value).base();

# Declarations

Call signature
template< no-throw-forward-range I, class T >
requires std::constructible_from<std::iter_value_t<I>, const T&>
I uninitialized_fill_n( I first, std::iter_difference_t<I> count,
const T& value );

(since C++20) (constexpr since C++26)

# Parameters

# Return value

As described above.

# Notes

An implementation may improve the efficiency of the ranges::uninitialized_fill_n, e.g. by using ranges::fill_n, if the value type of the output range is TrivialType.

# Example

#include <iostream>
#include <memory>
#include <string>
 
int main()
{
    constexpr int n{3};
    alignas(alignof(std::string)) char out[n * sizeof(std::string)];
 
    try
    {
        auto first{reinterpret_cast<std::string*>(out)};
        auto last = std::ranges::uninitialized_fill_n(first, n, "cppreference");
 
        for (auto it{first}; it != last; ++it)
            std::cout << *it << '\n';
 
        std::ranges::destroy(first, last);
    }
    catch (...)
    {
        std::cout << "Exception!\n";
    }
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3870C++20this algorithm might create objects on a const storagekept disallowed

# See also