命名空间
变体
操作

std::ranges::uninitialized_default_construct_n

来自 cppreference.cn
< cpp‎ | memory
 
 
内存管理库
(仅为说明目的*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
约束未初始化
内存算法
ranges::uninitialized_default_construct_n
(C++20) 
C 库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
定义于头文件 <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 起)
(constexpr 自 C++26 起)

在未初始化的内存区域 first + [0count) 中构造类型为 std::iter_value_t<I> 的对象,通过默认初始化,如同 return ranges::uninitialized_default_construct(std::counted_iterator(first, count),
                                               std::default_sentinel).base();
返回一样。

如果在初始化期间抛出异常,则已构造的对象将以未指定的顺序销毁。

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

目录

[编辑] 参数

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

[编辑] 返回值

如上所述。

[编辑] 复杂度

count 成线性关系。

[编辑] 异常

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

[编辑] 注解

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

[编辑] 注解

特性测试 Std 特性
__cpp_lib_raw_memory_algorithms 202411L (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 << "Exception!\n";
    }
 
    // For scalar types, uninitialized_default_construct_n
    // generally does not zero-fill the given uninitialized memory area.
    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)
    {
        // Maybe undefined behavior, pending CWG 1997:
        // for (const int i : v) { std::cout << i << ' '; }
        for (const int i : sample)
            std::cout << i << ' ';
    }
    else
        std::cout << "Unspecified!";
    std::cout << '\n';
}

可能的输出

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

[编辑] 缺陷报告

以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 3870 C++20 此算法可能在 const 存储上创建对象 保持不允许

[编辑] 参见

在由范围定义的未初始化内存区域中通过默认初始化构造对象
(算法函数对象)[编辑]
在由范围定义的未初始化内存区域中通过值初始化构造对象
(算法函数对象)[编辑]
在由起始位置和计数定义的未初始化内存区域中通过值初始化构造对象
(算法函数对象)[编辑]
在由起始位置和计数定义的未初始化内存区域中通过默认初始化构造对象
(函数模板) [编辑]