名稱空間
變體
操作

std::ranges::uninitialized_move, std::ranges::uninitialized_move_result

來自 cppreference.com
< cpp‎ | 記憶體
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(C++17)
(C++17)
(C++17)
受約束的未初始化
記憶體演算法
ranges::uninitialized_move
(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< std::input_iterator I, std::sentinel_for<I> S1,
          no-throw-forward-iterator O, no-throw-sentinel-for<O> S2 >
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_rvalue_reference_t<I>>
uninitialized_move_result<I, O>
    uninitialized_move( I ifirst, S1 ilast, O ofirst, S2 olast );
(1) (C++20 起)
(C++26 起為 constexpr)
template< ranges::input_range IR, no-throw-forward-range OR >
    requires std::constructible_from
                 <ranges::range_value_t<OR>,
                  ranges::range_rvalue_reference_t<IR>>
uninitialized_move_result<ranges::borrowed_iterator_t<IR>,
                          ranges::borrowed_iterator_t<OR>>
    uninitialized_move( IR&& in_range, OR&& out_range );
(2) (C++20 起)
(C++26 起為 constexpr)
輔助型別
template< class I, class O >
using uninitialized_move_result = ranges::in_out_result<I, O>;
(3) (C++20 起)

Nranges::min(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast))

1)N 個元素從 [ifirstilast) 移動(如果支援則使用移動語義)到未初始化的記憶體區域 [ofirstolast),如同:

for (; ifirst != ilast && ofirst != olast; ++ofirst, (void)++ifirst)
    ::new (voidify(*ofirst))
        std::remove_reference_t<std::iter_reference_t<O>>(ranges::iter_move(ifirst));
return {std::move(ifirst), ofirst};

如果在初始化期間丟擲異常,則已在 [ofirstolast) 中構造的物件將以未指定順序銷燬。此外,已移動的 [ifirstilast) 中的物件將處於有效但未指定的狀態。
2) 等效於 return ranges::uninitialized_move(ranges::begin(in_range), ranges::end(in_range),
                                  ranges::begin(out_range), ranges::end(out_range));

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

目錄

[編輯] 引數

ifirst, ilast - 定義要從中移動元素的輸入範圍的迭代器-哨兵對
in_range - 要從中移動元素的輸入range
ofirst, olast - 定義要初始化的輸出範圍的迭代器-哨兵對
out_range - 要初始化的輸出range

[編輯] 返回值

如上所述。

[編輯] 複雜度

N 成線性關係。

[編輯] 異常

在目標範圍內構造元素時丟擲的任何異常。

[編輯] 注意

如果輸出範圍的值型別是TrivialType,實現可以提高ranges::uninitialized_move的效率,例如透過使用ranges::copy_n

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

[編輯] 可能實現

struct uninitialized_move_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S1,
             no-throw-forward-iterator O, no-throw-sentinel-for<O> S2>
        requires std::constructible_from<std::iter_value_t<O>,
                                         std::iter_rvalue_reference_t<I>>
    constexpr ranges::uninitialized_move_result<I, O>
        operator()(I ifirst, S1 ilast, O ofirst, S2 olast) const
    {
        using ValueType = std::remove_reference_t<std::iter_reference_t<O>>;
        O current{ofirst};
        try
        {
            for (; !(ifirst == ilast or current == olast); ++ifirst, ++current)
                ::new (static_cast<void*>(std::addressof(*current))))
                    ValueType(ranges::iter_move(ifirst));
            return {std::move(ifirst), std::move(current)};
        }
        catch (...) // rollback: destroy constructed elements
        {
            for (; ofirst != current; ++ofirst)
                ranges::destroy_at(std::addressof(*ofirst));
            throw;
        }
    }
 
    template<ranges::input_range IR, no-throw-forward-range OR>
        requires std::constructible_from<ranges::range_value_t<OR>,
                                         ranges::range_rvalue_reference_t<IR>>
    constexpr ranges::uninitialized_move_result<ranges::borrowed_iterator_t<IR>,
                                                ranges::borrowed_iterator_t<OR>>
        operator()(IR&& in_range, OR&& out_range) const
    {
        return (*this)(ranges::begin(in_range), ranges::end(in_range),
                       ranges::begin(out_range), ranges::end(out_range));
    }
};
 
inline constexpr uninitialized_move_fn uninitialized_move{};

[編輯] 示例

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
 
void print(auto rem, auto first, auto last)
{
    for (std::cout << rem; first != last; ++first)
        std::cout << std::quoted(*first) << ' ';
    std::cout << '\n';
}
 
int main()
{
    std::string in[]{"Home", "World"};
    print("initially, in: ", std::begin(in), std::end(in));
 
    if (constexpr auto sz = std::size(in);
        void* out = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz))
    {
        try
        {
            auto first{static_cast<std::string*>(out)};
            auto last{first + sz};
            std::ranges::uninitialized_move(std::begin(in), std::end(in), first, last);
 
            print("after move, in: ", std::begin(in), std::end(in));
            print("after move, out: ", first, last);
 
            std::ranges::destroy(first, last);
        }
        catch (...)
        {
            std::cout << "Exception!\n";
        }
        std::free(out);
    }
}

可能的輸出

initially, in: "Home" "World"
after move, in: "" ""
after move, out: "Home" "World"

[編輯] 缺陷報告

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

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

[編輯] 參見

將多個物件移動到未初始化記憶體區域
(演算法函式物件)[編輯]
將一系列物件移動到未初始化記憶體區域
(函式模板) [編輯]