名稱空間
變體
操作

std::ranges::uninitialized_value_construct

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

[編輯] 參閱

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