命名空间
变体
操作

std::ranges::uninitialized_value_construct

来自 cppreference.cn
< cpp‎ | 内存
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(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>
调用签名 (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_value_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_value_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_value_construct(ranges::begin(r), ranges::end(r))

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

  • 调用它们中的任何一个时,不能指定显式模板参数列表。
  • 它们中的任何一个都对 参数依赖查找 不可见。
  • 当在函数调用运算符左侧的名称上,通过非限定查找找到其中任何一个时,实参依赖查找会被禁止。

目录

[编辑] 参数

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

[编辑] 返回值

如上所述。

[编辑] 复杂度

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

[编辑] 异常

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

[编辑] 注意

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

特性测试 标准 特性
__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++ 标准。

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

[编辑] 参阅

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