std::ranges::uninitialized_default_construct_n

来自cppreference.com
< cpp‎ | memory
 
 
内存管理库
(仅用于阐述*)
分配器
未初始化内存算法
受约束的未初始化内存算法
内存资源
未初始化存储 (C++20 前)
(C++17 弃用)
(C++17 弃用)
垃圾收集器支持 (C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
 
在标头 <memory> 定义
调用签名
template< no-throw-forward-iterator I >

    requires std::default_initializable<std::iter_value_t<I>>
I uninitialized_default_construct_n( I first,

                                     std::iter_difference_t<I> count );
(C++20 起)
(C++26 起为 constexpr)

如同用以下方式在未初始化内存区域 first + [0count) 上通过默认初始化构造 std::iter_value_t<I> 类型对象: return ranges::uninitialized_default_construct(std::counted_iterator(first, count),
                                               std::default_sentinel).base();

如果初始化中抛出了异常,那么以未指定的顺序销毁已构造的对象。

此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:

参数

first-要初始化的元素范围的起始
count-要构造的元素数

返回值

如上所述。

复杂度

count 成线性。

异常

构造目标范围中的元素时抛出的任何异常。

注解

如果默认初始化 std::iter_value_t<I> 对象时不调用非平凡的默认构造函数,那么实现可以跳过对象构造(而不更改可观察效果),这能由 std::is_trivially_default_constructible 检测。

功能特性测试标准功能特性
__cpp_lib_raw_memory_algorithms202411L(C++26)constexpr特化的内存算法

可能的实现

struct uninitialized_default_construct_n_fn
{
    template<no-throw-forward-iterator I>
        requires std::default_initializable<std::iter_value_t<I>>
    constexpr I operator()(I first, std::iter_difference_t<I> count) const
    {
        auto iter = std::counted_iterator(first, count);
        return ranges::uninitialized_default_construct(iter, std::default_sentinel).base();
    }
};
 
inline constexpr uninitialized_default_construct_n_fn uninitialized_default_construct_n{};

示例

#include <cstring>
#include <iostream>
#include <memory>
#include <string>
 
int main()
{
    struct S { std::string m{"█▓▒░ █▓▒░ "}; };
 
    constexpr int n{4};
    alignas(alignof(S)) char out[n * sizeof(S)];
 
    try
    {
        auto first{reinterpret_cast<S*>(out)};
        auto last = std::ranges::uninitialized_default_construct_n(first, n);
 
        auto count{1};
        for (auto it{first}; it != last; ++it)
            std::cout << count++ << ' ' << it->m << '\n';
 
        std::ranges::destroy(first, last);
    }
    catch (...)
    {
        std::cout << "异常!\n";
    }
 
    // 对于标量类型,uninitialized_default_construct_n
    // 通常不会以零填充给定的未初始化内存区域。
    constexpr int sample[]{1, 2, 3, 4, 5, 6};
    int v[]{1, 2, 3, 4, 5, 6};
    std::ranges::uninitialized_default_construct_n(std::begin(v), std::size(v));
    if (std::memcmp(v, sample, sizeof(v)) == 0)
    {
        // 可能为未定义行为,等待 CWG 1997 得到解决:
        // for (const int i : v) { std::cout << i << ' '; }
        for (const int i : sample)
            std::cout << i << ' ';
    }
    else
        std::cout << "未指定!";
    std::cout << '\n';
}

可能的输出:

1 █▓▒░ █▓▒░
2 █▓▒░ █▓▒░
3 █▓▒░ █▓▒░
4 █▓▒░ █▓▒░
1 2 3 4 5 6

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告应用于出版时的行为正确行为
LWG 3870C++20此算法可能在 const 存储上创建对象保持禁止

参阅

在范围所定义的未初始化内存中用默认初始化构造对象
(算法函数对象)
在范围所定义的未初始化内存中用值初始化构造对象
(算法函数对象)
在起点和数量所定义的未初始化内存中用值初始化构造对象
(算法函数对象)
在起点和数量所定义的未初始化内存中用默认初始化构造对象
(函数模板)