std::ranges::uninitialized_move, std::ranges::uninitialized_move_result
來自 cppreference.com
定義於標頭檔案 <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 起) |
設 N 為 ranges::min(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast))。
1) 將 N 個元素從
[
ifirst,
ilast)
移動(如果支援則使用移動語義)到未初始化的記憶體區域 [
ofirst,
olast)
,如同:
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};
如果在初始化期間丟擲異常,則已在
[
ofirst,
olast)
中構造的物件將以未指定順序銷燬。此外,已移動的 [
ifirst,
ilast)
中的物件將處於有效但未指定的狀態。2) 等效於 return ranges::uninitialized_move(ranges::begin(in_range), ranges::end(in_range),
ranges::begin(out_range), ranges::end(out_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 儲存上建立物件 | 保持不允許 |
[編輯] 參見
(C++20) |
將多個物件移動到未初始化記憶體區域 (演算法函式物件) |
(C++17) |
將一系列物件移動到未初始化記憶體區域 (函式模板) |