Przejdź do głównej zawartości

std::uninitialized_fill() algorithm

// (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) 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

    voidify(e)
    uwaga

    If 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.

Overload Resolution

These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od 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

ForwarditLegacyForwardIterator
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. (do 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 other ExecutionPolicy, 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

Main.cpp
#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);
}
Output
Example
Example
Example
Example
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::uninitialized_fill() algorithm

// (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) 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

    voidify(e)
    uwaga

    If 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.

Overload Resolution

These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od 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

ForwarditLegacyForwardIterator
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. (do 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 other ExecutionPolicy, 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

Main.cpp
#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);
}
Output
Example
Example
Example
Example
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.