名稱空間
變體
操作

std::ranges::uninitialized_default_construct

來自 cppreference.com
< 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 儲存上建立物件 保持不允許

[編輯] 參閱

透過預設初始化在由起始和計數定義的未初始化記憶體區域中構造物件
(演算法函式物件)[編輯]
在由範圍定義的未初始化記憶體區域中,透過值初始化構造物件
(演算法函式物件)[編輯]
在由起始和計數定義的未初始化記憶體區域中,透過值初始化構造物件
(演算法函式物件)[編輯]
透過預設初始化在由範圍定義的未初始化記憶體區域中構造物件
(函式模板) [編輯]