std::uninitialized_fill(3)
π
std::uninitialized_fill(3) β C++ Standard Libary bestie briefing π
okay so welcome to the std::uninitialized_fill(3) page from the C++ Standard Libary β header spelling and all π. Grab your iced coffee, bestie, because this one is lowkey iconic: it fills raw, uninitialized memory with a value and somehow makes exception safety look cute. π«Ά
πΈ NAME πΈ
std::uninitialized_fill - std::uninitialized_fill β thatβs the name, bestie, no plot twist π
β¨ Synopsis β¨
Defined in header <memory>
template< class ForwardIt, class T >
void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value );
1) okay so this one copies the given value into 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 */(e) is literally:
static_cast<void*>(&e) (until C++11)
static_cast<void*>(std::addressof(e)) (since C++11)
If an exception gets thrown during the initialization, the objects that were already constructed are destroyed in an unspecified order β no promises about order, bestie π.
template< class ExecutionPolicy, class ForwardIt, class T >
void uninitialized_fill( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, const T& value );
2) (since C++17) same as (1), but executed according to policy. This overload participates in overload resolution only if:
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.
(until C++20)
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.
(since C++20)
π Parameters π
- first, last β the range of the elements to initialize π±
- value β the value to construct the elements with β¨
- policy β the execution policy to use. See execution policy for details. π
πΈ Type requirements πΈ
ForwardItmust meet the requirements ofLegacyForwardIterator.- No increment, assignment, comparison, or indirection through valid instances of
ForwardItmay throw exceptions. Applying&*to aForwardItvalue must yield a pointer to its value type. (until C++11)
π« Return value π«
(none) β literally nothing comes back, bestie π«Ά
β‘ Complexity β‘
Linear in the distance between first and last. Slay, but make it O(n) π
π Exceptions π
The overload with a template parameter named ExecutionPolicy reports errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. π - If the algorithm fails to allocate memory,
std::bad_allocis thrown. π
π§ Possible implementation π§
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;
}
}
π Example π
Run this code, bestie β itβs giving βmanual object lifetime management but make it fashionβ π
#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
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards β yes, the lore got patched π₯
- LWG 866 β applied to C++98. Behavior as published: given
Tas the value type ofForwardIt, ifT::operator newexists, the program might be ill-formed. Correct behavior: uses global replacement-new instead. - LWG 2433 β applied to C++11. Behavior as published: this algorithm might be hijacked by overloaded
operator&. Correct behavior: usesstd::addressof. - LWG 3870 β applied to C++20. Behavior as published: this algorithm might create objects on a const storage. Correct behavior: kept disallowed.
π See also π
uninitialized_fill_nβ copies an object to an uninitialized area of memory, defined by a start and a count (function template)ranges::uninitialized_fill(C++20) β copies an object to an uninitialized area of memory, defined by a range (niebloid)
Source drop: http://cppreference.com, version dated 2024.06.10 β bye bestie, go fill that uninitialized memory responsibly πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms