Namespaces
Variants

std::ranges:: uninitialized_default_construct

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
定义于头文件 <memory>
调用签名
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) 在未初始化的内存区域 [ first , last ) 中通过 默认初始化 构造 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 - 待初始化元素的 range 范围

返回值

如上所述。

复杂度

first last 之间的距离呈线性关系。

异常

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

注释

若在默认初始化 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); // 跳过初始化
        I rollback{first};
        try
        {
            for (; !(first == last); ++first)
                ::new (static_cast<void*>(std::addressof(*first))) ValueType;
            return first;
        }
        catch (...) // 回滚:销毁已构造的元素
        {
            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"; }
    // 注意对于“平凡类型”,uninitialized_default_construct
    // 通常不会对给定的未初始化内存区域进行零填充。
    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 << "  ";
        // 可能是未定义行为,等待 CWG 1997 决议:
        // 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++ 标准。

DR 适用范围 发布时的行为 正确行为
LWG 3870 C++20 该算法可能在 const 存储上创建对象 保持禁止状态

参见

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