Namespaces
Variants

std::ranges::uninitialized_default_construct_n

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

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


 
 
定义于头文件 <memory>
调用签名
template< /*nothrow-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 );
(1) (C++20 起)
(constexpr 自 C++26 起)
template< /*execution-policy*/ Ep, /*nothrow-random-access-iterator*/ I >
    requires std::default_initializable<std::iter_value_t<I>>
I uninitialized_default_construct_n( Ep&& policy, I first,
                                     std::iter_difference_t<I> count );
(2) (C++26 起)

关于 /*execution-policy*/ 的定义,参见 此页面;关于其他仅用于阐述的概念的定义,参见 此页面.

1) 通过在目标范围 first + [0, count) 中默认初始化构造元素,如同由

return ranges::uninitialized_default_construct(std::counted_iterator(first, count),
                                               std::default_sentinel).base();

若在初始化期间抛出异常,则已构造的对象将以未指定的顺序被销毁。
2) 与 (1) 相同,但按照 policy 执行。

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

参数

first - 要初始化的元素范围的起始
count - 要构造的元素的数量
policy - 要使用的执行策略

返回值

如上所述。

异常

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

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

注解

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

功能测试 宏 值 标准 特性
__cpp_lib_parallel_algorithm 202506L (C++26) 并行范围算法
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用于 特殊化 <memory> 算法, (1)

可能的实现

struct uninitialized_default_construct_n_fn
{
    template</*nothrow-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

另请参阅

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