std::uninitialized_fill() algorithm
- since C++17
- until C++17
// (1)
template< class ForwardIt, class T >
void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value );
// (2)
template< class ExecutionPolicy, class ForwardIt, class T >
void uninitialized_fill( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value );
// (1)
template< class ForwardIt, class T >
void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value );
-
(1) Copies the given
value
to an uninitialized memory area, defined by the range [first
,last
] as if by:for (; first != last; ++first)
::new (/* VOIDIFY */(*first))
typename std::iterator_traits<ForwardIt>::value_type(value);Where
/* VOIDIFY */
is- since C++20
- since C++11
- until C++11
voidify(e)
static_cast<void*>(std::addressof(e))
static_cast<void*>(&e)
cautionIf an exception is thrown during the initialization, the objects already constructed are destroyed in an unspecified order.
-
(2) Same as (1), but executed according to
policy
.
These overloads participate in overload resolution only if
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) is true
.
Parameters
first last | The range of elements to initialize. |
value | The value to fill the range with. |
policy | The execution policy to use. See execution policy for details. |
Type requirements
Forwardit | LegacyForwardIterator |
ForwardIt NoThrowForwardIt | LegacyForwardIterator |
No increment, assignment, comparison, or indirection through valid instances of NoThrowForwardIt
may throw exceptions.
Applying &*
to a NoThrowForwardIt
value must yield a pointer to its value type. (until C++11)
Return value
(none)
Complexity
Linear in the distance between first
and last
.
Exceptions
The overloads with a template parameter named ExecutionPolicy
report errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicy
is one of the standard policies,std::terminate
is called. For none otherExecutionPolicy
, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_alloc
is thrown.
Possible implementation
uninitialized_fill (1)
template<class ForwardIt, class T>
void uninitialized_fill(ForwardIt first, ForwardIt last, const T& value)
{
using V = typename std::iterator_traits<ForwardIt>::value_type;
ForwardIt current = first;
try
{
for (; current != last; ++current)
::new (static_cast<void*>(std::addressof(*current))) V(value);
}
catch (...)
{
for (; first != current; ++first)
first->~V();
throw;
}
}
Examples
#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);
}
Example
Example
Example
Example
Hover to see the original license.