命名空间
变体
操作

std::ranges::uninitialized_value_construct

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

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

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

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

    uninitialized_value_construct( R&& r );
(2) (自 C++20 起)
(constexpr 自 C++26 起)
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_value_construct(ranges::begin(r), ranges::end(r))

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

目录

[编辑] 参数

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

[编辑] 返回值

如上所述。

[编辑] 复杂度

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

[编辑] 异常

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

[编辑] 注解

如果范围的值类型是 CopyAssignable TrivialType,则实现可以提高 ranges::uninitialized_value_construct 的效率,例如通过使用 ranges::fill

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

[编辑] 可能的实现

struct uninitialized_value_construct_fn
{
    template<no-throw-forward-iterator I, no-throw-sentinel-for<I> S>
        requires std::value_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::fill(first, last, ValueType());
        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_value_construct_fn uninitialized_value_construct{};

[编辑] 示例

#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_value_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";
    }
 
    // For scalar types, uninitialized_value_construct
    // zero-fills the given uninitialized memory area.
    int v[]{0, 1, 2, 3};
    std::cout << ' ';
    for (const int i : v)
        std::cout << ' ' << static_cast<char>(i + 'A');
    std::cout << "\n ";
    std::ranges::uninitialized_value_construct(std::begin(v), std::end(v));
    for (const int i : v)
        std::cout << ' ' << static_cast<char>(i + 'A');
    std::cout << '\n';
}

输出

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

[编辑] 缺陷报告

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

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

[编辑] 参见

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