命名空间
变体
操作

std::uninitialized_default_construct_n

来自 cppreference.cn
< cpp‎ | 内存
 
 
内存管理库
(仅用于说明*)
未初始化内存算法
(C++17)
(C++17)
uninitialized_default_construct_n
(C++17)
(C++17)
约束未初始化
内存算法
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 中弃用*)
(在 C++20 中弃用*)
显式生命周期管理
 
定义于头文件 <memory>
template< class NoThrowForwardIt, class Size >

NoThrowForwardIt uninitialized_default_construct_n

    ( NoThrowForwardIt first, Size count );
(1) (自 C++17 起)
(自 C++26 起为 constexpr)
template< class ExecutionPolicy, class NoThrowForwardIt, class Size >

NoThrowForwardIt uninitialized_default_construct_n

    ( ExecutionPolicy&& policy, NoThrowForwardIt first, Size count );
(2) (自 C++17 起)
1) 在未初始化的内存区域 first + [0count) 中构造类型为 typename std::iterator_traits<NoThrowForwardIt>::value_type 的对象,通过默认初始化,如同通过以下方式:

for (; count > 0; (void)++first, --count)
    ::new (voidify(*first))
        typename std::iterator_traits<NoThrowForwardIt>::value_type;
return first;

如果在初始化期间抛出异常,则已构造的对象将以未指定的顺序销毁。
2)(1) 相同,但根据 policy 执行。
仅当满足以下所有条件时,此重载才参与重载解析

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(直到 C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(自 C++20 起)

内容

[编辑] 参数

first - 要初始化的元素范围的开始
count - 要构造的元素数量
policy - 要使用的执行策略
类型要求
-
NoThrowForwardIt 必须满足 LegacyForwardIterator 的要求。
-
通过 NoThrowForwardIt 的有效实例进行的递增、赋值、比较或间接引用都不得抛出异常。

[编辑] 返回值

如上所述。

[编辑] 复杂度

n 中呈线性。

[编辑] 异常

具有名为 ExecutionPolicy 的模板参数的重载按如下方式报告错误

  • 如果作为算法一部分调用的函数的执行抛出异常,并且 ExecutionPolicy标准策略之一,则调用 std::terminate。 对于任何其他 ExecutionPolicy,行为是实现定义的。
  • 如果算法无法分配内存,则会抛出 std::bad_alloc

[编辑] 注释

特性测试 Std 特性
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用于 专用内存算法(1)

[编辑] 可能的实现

template<class NoThrowForwardIt, class Size>
constexpr ForwardIt uninitialized_default_construct_n(NoThrowForwardIt first, Size count)
{
    using T = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = first;
 
    try
    {
        for (; countn > 0; (void) ++current, --count)
            ::new (static_cast<void*>(std::addressof(*current))) T;
        return current;
    }
    catch (...)
    {
        std::destroy(first, current);
        throw;
    }
}

[编辑] 示例

#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";
    }
 
    // For scalar types, 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 to be resolved:
    // for (const int i : v)
    //     std::cout << i << ' ';
 
    // The result is unspecified:
    std::cout << (std::memcmp(v, original, sizeof(v)) == 0 ? "un" : "") << "modified\n";
}

可能的输出

default value
default value
default value
unmodified

[编辑] 缺陷报告

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

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

[编辑] 参见

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