std::uninitialized_fill

Header: <memory>

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

# Declarations

template< class NoThrowForwardIt, class T >
void uninitialized_fill( NoThrowForwardIt first,
NoThrowForwardIt last, const T& value );

(constexpr since C++26)

template< class ExecutionPolicy, class NoThrowForwardIt, class T >
void uninitialized_fill( ExecutionPolicy&& policy,
NoThrowForwardIt first,
NoThrowForwardIt last, const T& value );

(since C++17)

# Parameters

# 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>
 
int main()
{
    const std::size_t sz = 4;
    std::allocator<std::string> alloc;
    std::string* p = alloc.allocate(sz);
 
    std::uninitialized_fill(p, p + sz, "Example");
 
    for (std::string* i = p; i != p + sz; ++i)
    {
        std::cout << *i << '\n';
        i->~basic_string<char>();
    }
 
    alloc.deallocate(p, sz);
}

# 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 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