命名空间
变体
操作

std::uninitialized_fill

来自 cppreference.cn
< cpp‎ | 内存
 
 
内存管理库
(仅作说明*)
未初始化内存算法
uninitialized_fill
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
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< class NoThrowForwardIt, class T >

void uninitialized_fill( NoThrowForwardIt first,

                         NoThrowForwardIt last, const T& value );
(1) (C++26 起为 constexpr)
template< class ExecutionPolicy, class NoThrowForwardIt, class T >

void uninitialized_fill( ExecutionPolicy&& policy,
                         NoThrowForwardIt first,

                         NoThrowForwardIt last, const T& value );
(2) (C++17 起)
1) 复制 value 到未初始化内存区域 [firstlast),如同通过以下方式:

for (; first != last; ++first)
    ::new (voidify(*first))
        typename std::iterator_traits<NoThrowForwardIt>::value_type(value);

如果在初始化期间抛出异常,则已构造的对象将以未指定顺序销毁。
2)(1),但根据 policy 执行。
仅当满足以下所有条件时,此重载才参与重载决议

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

(C++20 前)

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

(C++20 起)

目录

[edit] 参数

first, last - 定义要初始化的元素范围的迭代器对
value - 用于构造元素的值
policy - 要使用的 执行策略
类型要求
-
NoThrowForwardIt 必须满足 LegacyForwardIterator 的要求。
-
NoThrowForwardIt 的有效实例进行增量、赋值、比较或间接引用不能抛出异常。&* 应用于 NoThrowForwardIt 值必须产生指向其值类型的指针。(直至 C++11)

[edit] 复杂度

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

[edit] 异常

带有名为 ExecutionPolicy 的模板参数的重载会按如下方式报告错误:

  • 如果作为算法一部分调用的函数执行抛出异常且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的。
  • 如果算法未能分配内存,则抛出 std::bad_alloc

[edit] 注意

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

[edit] 可能的实现

template<class NoThrowForwardIt, class T>
constexpr void uninitialized_fill(NoThrowForwardIt first, NoThrowForwardIt last,
                                  const T& value)
{
    using V = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = first;
    try
    {
        for (; current != last; ++current)
            ::new (static_cast<void*>(std::addressof(*current))) V(value);
    } 
    catch (...)
    {
        for (; first != current; ++first)
            first->~V();
        throw;
    }
}

[edit] 示例

#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
 
int main()
{
    const std::size_t sz = 4;
    std::allocator<std::string> alloc;
    std::string* p = alloc.allocate(sz);
 
    std::uninitialized_fill(p, p + sz, "Example");
 
    for (std::string* i = p; i != p + sz; ++i)
    {
        std::cout << *i << '\n';
        i->~basic_string<char>();
    }
 
    alloc.deallocate(p, sz);
}

输出

Example
Example
Example
Example

[edit] 缺陷报告

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

缺陷报告 应用于 发布时的行为 正确的行为
LWG 866 C++98 给定 TNoThrowForwardIt 的值类型,如果
T::operator new 存在,程序可能格式错误
改为使用全局 placement new
LWG 2433 C++11 此算法可能被重载的 operator& 劫持 使用 std::addressof
LWG 3870 C++20 此算法可能在 const 存储上创建对象 保持不允许

[edit] 另请参阅

将对象复制到由起始和计数定义的未初始化内存区域
(函数模板) [编辑]
将对象复制到由范围定义的未初始化内存区域
(算法函数对象)[编辑]