std::ranges...lized_fill_n(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 initialize
  • n β€” number of elements to construct
  • x β€” 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

🫢 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