命名空间
变体
操作

std::ranges::uninitialized_default_construct

来自 cppreference.cn
< cpp‎ | 内存
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
ranges::uninitialized_default_construct
(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>
调用签名 (Call signature)
template< no-throw-forward-iterator I, no-throw-sentinel-for<I> S >

    requires std::default_initializable<std::iter_value_t<I>>

I uninitialized_default_construct( I first, S last );
(1) (C++20 起)
(C++26 起为 constexpr)
template< no-throw-forward-range R >

    requires std::default_initializable<ranges::range_value_t<R>>
ranges::borrowed_iterator_t<R>

    uninitialized_default_construct( R&& r );
(2) (C++20 起)
(C++26 起为 constexpr)
1) 在未初始化的内存区域 [firstlast) 中,通过默认初始化构造 std::iter_value_t<I> 类型的对象,如同通过

for (; first != last; ++first)
    ::new (voidify(*first))
        std::remove_reference_t<std::iter_reference_t<I>>;
return first;

如果在初始化期间抛出异常,则已构造的对象将以未指定顺序销毁。
2) 等价于 ranges::uninitialized_default_construct(ranges::begin(r), ranges::end(r))

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

目录

[编辑] 参数

first, last - 定义要初始化的元素范围的迭代器-哨兵对
r - 要初始化的元素的范围

[编辑] 返回值

如上所述。

[编辑] 复杂度

firstlast 之间的距离呈线性关系。

[编辑] 异常

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

[编辑] 注意

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

特性测试 标准 特性
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用于未初始化内存算法(1,2)

[编辑] 可能的实现

struct uninitialized_default_construct_fn
{
    template<no-throw-forward-iterator I, no-throw-sentinel-for<I> S>
        requires std::default_initializable<std::iter_value_t<I>>
    constexpr I operator()(I first, S last) const
    {
        using ValueType = std::remove_reference_t<std::iter_reference_t<I>>;
        if constexpr (std::is_trivially_default_constructible_v<ValueType>)
            return ranges::next(first, last); // skip initialization
        I rollback{first};
        try
        {
            for (; !(first == last); ++first)
                ::new (static_cast<void*>(std::addressof(*first))) ValueType;
            return first;
        }
        catch (...) // rollback: destroy constructed elements
        {
            for (; rollback != first; ++rollback)
                ranges::destroy_at(std::addressof(*rollback));
            throw;
        }
    }
 
    template<no-throw-forward-range R>
        requires std::default_initializable<ranges::range_value_t<R>>
    constexpr ranges::borrowed_iterator_t<R> operator()(R&& r) const
    {
        return (*this)(ranges::begin(r), ranges::end(r));
    }
};
 
inline constexpr uninitialized_default_construct_fn uninitialized_default_construct{};

[编辑] 示例

#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{first + n};
 
        std::ranges::uninitialized_default_construct(first, last);
 
        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"; }
 
    // Notice that for "trivial types" the uninitialized_default_construct
    // generally does not zero-fill the given uninitialized memory area.
    constexpr char sample[]{'A', 'B', 'C', 'D', '\n'};
    char v[]{'A', 'B', 'C', 'D', '\n'};
    std::ranges::uninitialized_default_construct(std::begin(v), std::end(v));
    if (std::memcmp(v, sample, sizeof(v)) == 0)
    {
        std::cout << "  ";
        // Maybe undefined behavior, pending CWG 1997 to be resolved:
        // for (const char c : v) { std::cout << c << ' '; }
        for (const char c : sample)
            std::cout << c << ' ';
    }
    else
        std::cout << "Unspecified\n";
}

可能的输出

1 ▄▀▄▀▄▀▄▀
2 ▄▀▄▀▄▀▄▀
3 ▄▀▄▀▄▀▄▀
4 ▄▀▄▀▄▀▄▀
  A B C D

[编辑] 缺陷报告

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

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

[编辑] 参阅

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