Namespaces
Variants

std::uninitialized_value_construct_n

来自 zh.cppreference.net
 
 
算法库
受约束算法及范围算法 (C++20)
受约束算法,例如 ranges::copy、ranges::sort……
排序及相关操作
划分操作
(C++11)    

排序操作
二分查找操作
(在已划分范围上)
集合操作 (在已排序范围上)
合并操作 (在已排序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
 
定义于头文件 <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 + [0, count)值初始化在目标范围 中构造元素,如同通过

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

若初始化期间抛出异常,则已构造的对象将以未指定的顺序销毁。
2) 同 (1),但按 policy 执行。
此重载仅在下述表达式的值为 true 时参与重载决议:

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

(C++20 前)

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

(C++20 起)

参数

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

返回值

如上所述。

异常

2) 在执行过程中:
  • 如果并行化所需的临时内存资源不可用,std::bad_alloc 会被抛出。
  • 如果在通过算法参数访问对象时抛出未捕获的异常,其行为由执行策略决定(对于标准策略,会调用std::terminate)。

注解

功能测试宏 值 标准 功能
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr for 特化的<memory>算法,(1)

可能的实现

template<class NoThrowForwardIt, class Size>
constexpr ForwardIt uninitialized_value_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 <iostream>
#include <memory>
#include <string>
int main()
{
    struct S { std::string m{"Default value"}; };
    constexpr int n{3};
    alignas(alignof(S)) unsigned char 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 << "Exception!\n";
    }
    // 对于标量类型,uninitialized_value_construct_n
    // 会将给定的未初始化内存区域进行零初始化。
    int v[]{1, 2, 3, 4};
    for (const int i : v)
        std::cout << i << ' ';
    std::cout << '\n';
    std::uninitialized_value_construct_n(std::begin(v), std::size(v));
    for (const int i : v)
        std::cout << i << ' ';
    std::cout << '\n';
}

输出:

Default value
Default value
Default value
1 2 3 4
0 0 0 0

参见

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