std::uninitialized_value_construct_n

来自cppreference.com
< cpp‎ | memory
 
 
内存管理库
(仅用于阐述*)
分配器
未初始化内存算法
(C++17)
uninitialized_value_construct_n
(C++17)
(C++17)
(C++17)
受约束的未初始化内存算法
内存资源
未初始化存储(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<class NoThrowForwardIt, class Size >

NoThrowForwardIt uninitialized_value_construct_n

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

NoThrowForwardIt uninitialized_value_construct_n

    ( ExecutionPolicy&& policy, NoThrowForwardIt first, Size count );
(2) (C++17 起)
1) 如同用以下方式在未初始化内存区域 first + [0count) 上通过值初始化构造 typenamestd::iterator_traits<NoThrowForwardIt>::value_type 类型对象:

for(; count >0;(void)++first, --count)
    ::new(voidify(*first))
        typenamestd::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 合法实例的自增、复制、比较或间接均不抛出异常。

[编辑]返回值

如上所述。

[编辑]复杂度

count 成线性。

[编辑]异常

拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

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

[编辑]注解

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

[编辑]可能的实现

template<class NoThrowForwardIt, class Size>constexpr ForwardIt uninitialized_value_construct_n(NoThrowForwardIt first, Size count){using T =typenamestd::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 <iostream>#include <memory>#include <string>   int main(){struct S {std::string m{"默认值"};};   constexprint n{3}; alignas(alignof(S))unsignedchar mem[n * sizeof(S)];   try{auto first{reinterpret_cast<S*>(mem)};auto last = std::uninitialized_value_construct_n(first, n);   for(auto it{first}; it != last;++it)std::cout<< it->m <<'\n';   std::destroy(first, last);}catch(...){std::cout<<"异常!\n";}   // 对于标量类型,uninitialized_value_construct_n 会以零填充给定的未初始化内存区域。int v[]{1, 2, 3, 4};for(constint i : v)std::cout<< i <<' ';std::cout<<'\n'; std::uninitialized_value_construct_n(std::begin(v), std::size(v));for(constint i : v)std::cout<< i <<' ';std::cout<<'\n';}

输出:

默认值 默认值 默认值 1 2 3 4 0 0 0 0

[编辑]缺陷报告

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

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

[编辑]参阅

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