std::uninitialized_fill_n

Header: <memory>

  1. Copies value to an uninitialized memory area first+[0,count) as if by for (; count–; ++first) ::new (voidify(*first)) typename std::iterator_traits::value_type(value); return first;

# Declarations

template< class NoThrowForwardIt, class Size, class T >
NoThrowForwardIt uninitialized_fill_n( NoThrowForwardIt first,
Size count, const T& value );

(constexpr since C++26)

template< class ExecutionPolicy,
class NoThrowForwardIt, class Size, class T >
NoThrowForwardIt uninitialized_fill_n( ExecutionPolicy&& policy,
NoThrowForwardIt first,
Size count, const T& value );

(since C++17)

# Parameters

# Return value

As described above.

# Notes

Feature-test macro Value Std Feature __cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr for specialized memory algorithms, (1)

# Example

#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
 
int main()
{
    std::string* p;
    std::size_t sz;
    std::tie(p, sz) = std::get_temporary_buffer<std::string>(4);
    std::uninitialized_fill_n(p, sz, "Example");
 
    for (std::string* i = p; i != p + sz; ++i)
    {
        std::cout << *i << '\n';
        i->~basic_string<char>();
    }
    std::return_temporary_buffer(p);
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 866C++98given T as the value type of NoThrowForwardIt, ifT::operator new exists, the program might be ill-formeduses global placement new instead
LWG 1339C++98the location of the first element followingthe filling range was not returnedreturned
LWG 2433C++11this algorithm might be hijacked by overloaded operator&uses std::addressof
LWG 3870C++20this algorithm might create objects on a const storagekept disallowed

# See also