std::ranges...lized_fill_n(3)
π std::ranges::uninitialized_fill_n π
okay so bestie, we're in the C++ Standard Libary manual (that's the manual name, fr fr), and this page is giving uninitialized-memory main character energy. The entity is std::ranges::uninitialized_fill_n β yes, the name section is literally the same thing twice, slay β¨.
πΈ NAME πΈ
std::ranges::uninitialized_fill_n - std::ranges::uninitialized_fill_n
β¨ Synopsis β¨
Defined in header <memory>.
Call signature:
template< no-throw-forward-range I, class T >
requires std::constructible_from<std::iter_value_t<I>, const T&>
(since C++20)
I uninitialized_fill_n( I first, std::iter_difference_t<I> n, const
T& x );
π§ Description π§
so this one constructs n copies of the given value x in an uninitialized memory area, designated by the range [first, first + n), as if by:
for (; n--; ++first) {
::new (static_cast<void*>(std::addressof(*first)))
std::remove_reference_t<std::iter_reference_t<I>>(x);
}
and bestie, if an exception is thrown during the initialization, the objects already constructed are destroyed in an unspecified order. no cap, that's the rollback vibe π.
The function-like entities described on this page are niebloids, which means:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
π Parameters π
firstβ the beginning of the range of the elements to initializenβ number of elements to constructxβ the value to construct the elements with
π¦ Return value π¦
An iterator equal to first + n. periodt.
π Complexity π
Linear in n.
π Exceptions π
The exception thrown on construction of the elements in the destination range, if any.
π₯ Notes π₯
An implementation may improve the efficiency of the ranges::uninitialized_fill_n, e.g. by using ranges::fill_n, if the value type of the output range is TrivialType. cute optimization moment β¨.
π Possible implementation π
struct uninitialized_fill_n_fn
{
template<no-throw-forward-range I, class T>
requires std::constructible_from<std::iter_value_t<I>, const T&>
I operator()(I first, std::iter_difference_t<I> n, const T& x) const
{
I rollback{first};
try
{
for (; n-- > 0; ++first)
ranges::construct_at(std::addressof(*first), x);
return first;
}
catch (...) // rollback: destroy constructed elements
{
for (; rollback != first; ++rollback)
ranges::destroy_at(std::addressof(*rollback));
throw;
}
}
};
inline constexpr uninitialized_fill_n_fn uninitialized_fill_n{};
π Example π
Run this code, bestie:
#include <iostream>
#include <memory>
#include <string>
int main()
{
constexpr int n{3};
alignas(alignof(std::string)) char out[n * sizeof(std::string)];
try
{
auto first{reinterpret_cast<std::string*>(out)};
auto last = std::ranges::uninitialized_fill_n(first, n,
"cppreference");
for (auto it{first}; it != last; ++it)
std::cout << *it << '\n';
std::ranges::destroy(first, last);
}
catch (...)
{
std::cout << "Exception!\n";
}
}
Output:
cppreference
cppreference
cppreference
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 3870- Applied to:
C++20 - Behavior as published: this algorithm might create objects on a const storage
- Correct behavior: kept disallowed
- Applied to:
π«Ά See also π«Ά
ranges::uninitialized_fillβ copies an object to an uninitialized area of memory, defined by a range (C++20) (niebloid)uninitialized_fill_nβ copies an object to an uninitialized area of memory, defined by a start and a count (function template)
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::ranges...lized_fill_n(3) β okay bye bestie, go initialize that 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