名稱空間
變體
操作

std::uninitialized_default_construct

來自 cppreference.com
< cpp‎ | 記憶體
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
uninitialized_default_construct
(C++17)
(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 >

void uninitialized_default_construct( NoThrowForwardIt first,

                                      NoThrowForwardIt last );
(1) (C++17 起)
(C++26 起為 constexpr)
template< class ExecutionPolicy, class NoThrowForwardIt >

void uninitialized_default_construct( ExecutionPolicy&& policy,
                                      NoThrowForwardIt first,

                                      NoThrowForwardIt last );
(2) (C++17 起)
1) 在未初始化記憶體區域 [firstlast) 中,透過預設初始化構造 typename std::iterator_traits<NoThrowForwardIt>::value_type 型別的物件,如同

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

如果在初始化期間丟擲異常,則已構造的物件將以未指定順序銷燬。
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 起)

目錄

[編輯] 引數

first, last - 定義要初始化的元素範圍的迭代器對
policy - 要使用的 執行策略
型別要求
-
NoThrowForwardIt 必須滿足 LegacyForwardIterator 的要求。
-
透過 `NoThrowForwardIt` 的有效例項,不允許遞增、賦值、比較或間接引用丟擲異常。

[編輯] 複雜度

firstlast 之間的距離呈線性關係。

[編輯] 異常

帶有名為 ExecutionPolicy 的模板引數的過載會按如下方式報告錯誤:

  • 若作為演算法一部分呼叫的函式執行丟擲異常且 `ExecutionPolicy` 是標準策略之一,則呼叫 std::terminate。對於任何其他 `ExecutionPolicy`,行為是實現定義的。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[編輯] 注意

特性測試 標準 特性
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 對於未初始化記憶體演算法(1)

[編輯] 可能的實現

template<class NoThrowForwardIt>
constexpr void uninitialized_default_construct(NoThrowForwardIt first,
                                               NoThrowForwardIt last)
{
    using Value = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = first;
    try
    {
        for (; current != last; ++current)
        {
            ::new (static_cast<void*>(std::addressof(*current))) Value;
        }
    }
    catch (...)
    {
        std::destroy(first, current);
        throw;
    }
}

[編輯] 示例

#include <cstring>
#include <iostream>
#include <memory>
#include <string>
 
struct S
{
    std::string m{"Default value"};
};
 
int main()
{
    constexpr int n{3};
    alignas(alignof(S)) unsigned char mem[n * sizeof(S)];
 
    try
    {
        auto first{reinterpret_cast<S*>(mem)};
        auto last{first + n};
 
        std::uninitialized_default_construct(first, last);
 
        for (auto it{first}; it != last; ++it)
            std::cout << it->m << '\n';
 
        std::destroy(first, last);
    }
    catch (...)
    {
        std::cout << "Exception!\n";
    }
 
    // For scalar types, uninitialized_default_construct
    // generally does not zero-fill the given uninitialized memory area.
    int v[]{1, 2, 3, 4};
    const int original[]{1, 2, 3, 4};
    std::uninitialized_default_construct(std::begin(v), std::end(v));
 
    // Maybe undefined behavior, pending CWG 1997 to be resolved.
    // for (const int i : v)
    //     std::cout << i << ' ';
 
    // The result is unspecified.
    std::cout <<
        (std::memcmp(v, original, sizeof(v)) == 0 ? "Unmodified\n" : "Modified\n");
}

可能的輸出

Default value
Default value
Default value
Unmodified

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 3870 C++20 此演算法可能在 const 儲存上建立物件 保持不允許

[編輯] 參閱

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