std::uninit...construct_n(3)

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

πŸ’– NAME πŸ’–

okay so bestie, this page is std::uninit...construct_n(3) from the C++ Standard Libary manual. The NAME line is literally std::uninitialized_default_construct_n β€” std::uninitialized_default_construct_n (yes, twice, it’s giving duplicate-key main character energy). No cap, this is the β€œconstruct n fresh objects in uninitialized storage by default-initialization” algorithm, and it’s lowkey iconic for placement-new girlies. πŸ’…


✨ Synopsis ✨

Defined in header <memory>

template< class ForwardIt, class Size >
ForwardIt uninitialized_default_construct_n( ForwardIt first, Size n );

(1) (since C++17)

template< class ExecutionPolicy, class ForwardIt, class Size >
ForwardIt uninitialized_default_construct_n( ExecutionPolicy&& policy,
                                             ForwardIt first, Size n );

(2) (since C++17)

  1. Constructs n objects of type typename std::iterator_traits<ForwardIt>::value_type in the uninitialized storage starting at first by default-initialization, as if by:
for (; n > 0; (void) ++first, --n)
    ::new (static_cast<void*>(std::addressof(*first)))
        typename std::iterator_traits<ForwardIt>::value_type;

If an exception is thrown during the initialization, the objects already constructed are destroyed in an unspecified order.

  1. 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 β€” the beginning of the range of elements to initialize.
  • n β€” the number of elements to construct.
  • policy β€” the execution policy to use. See execution policy for details.

πŸ¦‹ Type requirements πŸ¦‹

  • ForwardIt must meet the requirements of LegacyForwardIterator.
  • No increment, assignment, comparison, or indirection through valid instances of ForwardIt may throw exceptions.

πŸŽ€ Return value πŸŽ€

The end of the range of objects (i.e., std::next(first, n)).


⭐ Complexity ⭐

Linear in 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 ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

🧚 Possible implementation 🧚

Here’s the possible implementation, every token intact:

template<class ForwardIt, class Size>
ForwardIt uninitialized_default_construct_n(ForwardIt first, Size n)
{
    using T = typename std::iterator_traits<ForwardIt>::value_type;
    ForwardIt current = first;

    try
    {
        for (; n > 0; (void) ++current, --n)
            ::new (const_cast<void*>(static_cast<const volatile void*>(
                std::addressof(*current)))) T;
        return current;
    }
    catch (...)
    {
        std::destroy(first, current);
        throw;
    }
}

🌈 Example 🌈

This example ate, fr fr:

// Run this code

#include <cstring>
#include <iostream>
#include <memory>
#include <string>

struct S
{
    std::string m{"default value"};
};

int main()
{
    constexpr int n{3};
    alignas(alignof(S)) unsigned char mem[n * sizeof(S)];

    try
    {
        auto first{reinterpret_cast<S*>(mem)};
        auto last = std::uninitialized_default_construct_n(first, n);

        for (auto it{first}; it != last; ++it)
            std::cout << it->m << '\n';

        std::destroy(first, last);
    }
    catch(...)
    {
        std::cout << "Exception!\n";
    }

    // Notice that for "trivial types" the uninitialized_default_construct_n
    // generally does not zero-initialize the given uninitialized memory area.
    int v[]{1, 2, 3, 4};
    const int original[]{1, 2, 3, 4};
    std::uninitialized_default_construct_n(std::begin(v), std::size(v));

    // An attempt to access v might be an undefined behavior, pending CWG 1997:
    // for (const int i : v)
    //     std::cout << i << ' ';

    // The result is unspecified:
    std::cout << (std::memcmp(v, original, sizeof(v)) == 0 ? "un" : "")
              << "modified\n";
}

πŸ“ Possible output πŸ“

default value
default value
default value
unmodified

πŸ’€ 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 πŸ‘‘

  • uninitialized_default_construct (C++17) β€” constructs objects by default-initialization in an uninitialized area of memory, defined by a range (function template).
  • uninitialized_value_construct_n (C++17) β€” constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count (function template).
  • ranges::uninitialized_default_construct_n (C++20) β€” constructs objects by default-initialization in an uninitialized area of memory, defined by a start and count (niebloid).

🫢 that’s the whole page, bestie. source: http://cppreference.com β€’ version/date: 2024.06.10 β€’ std::uninit...construct_n(3) out πŸ’…

πŸ“¦ where this page comes from