std::uninitialized_fill
Min standard notice:
Header: <memory>
- 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
first, last: the range of the elements to initializevalue: the value to construct the elements withpolicy: the execution policy to use
# 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
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 866 | C++98 | given T as the value type of NoThrowForwardIt, ifT::operator new exists, the program might be ill-formed | uses global placement new instead |
| LWG 2433 | C++11 | this algorithm might be hijacked by overloaded operator& | uses std::addressof |
| LWG 3870 | C++20 | this algorithm might create objects on a const storage | kept disallowed |