std::ranges::uninitialized_move_n, std::ranges::uninitialized_move_n_result
定義於標頭檔案 <memory> |
||
呼叫簽名 (Call signature) |
||
template< std::input_iterator I, no-throw-forward-iterator O, no-throw-sentinel-for<O> S > |
(1) | (C++20 起) (C++26 起為 constexpr) |
輔助型別 |
||
template< class I, class O > using uninitialized_move_n_result = ranges::in_out_result<I, O>; |
(2) | (C++20 起) |
設 N 為 ranges::min(count, ranges::distance(ofirst, olast))。
將從 ifirst 開始的範圍中的 N 個元素(如果支援則使用移動語義)複製到未初始化的記憶體區域 [
ofirst,
olast)
,如同透過 auto ret = ranges::uninitialized_move(std::counted_iterator(std::move(ifirst), count),
std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};。
如果在初始化期間丟擲異常,則已在 [
ofirst,
olast)
中構造的物件將以未指定順序銷燬。此外,輸入範圍中從 ifirst 開始且已移動的物件將處於有效但未指定的狀態。
如果 [
ofirst,
olast)
與 ifirst +
[
0,
count)
重疊,則行為未定義。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
ifirst | - | 要移動的元素輸入範圍的開始 |
ofirst, olast | - | 定義要初始化的元素輸出範圍的迭代器-哨兵對 |
n | - | 要移動的元素數量 |
[編輯] 返回值
如上所述。
[編輯] 複雜度
𝓞(N)。
[編輯] 異常
在目標範圍內構造元素時丟擲的任何異常。
[編輯] 注意
如果輸出範圍的值型別是TrivialType,則實現可以提高 ranges::uninitialized_move_n
的效率,例如透過使用 ranges::copy_n。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 對於未初始化記憶體演算法,(1) |
[編輯] 可能的實現
struct uninitialized_move_n_fn { template<std::input_iterator I, no-throw-forward-iterator O, no-throw-sentinel-for<O> S> requires std::constructible_from<std::iter_value_t<O>, std::iter_rvalue_reference_t<I>> constexpr ranges::uninitialized_move_n_result<I, O> operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const { auto iter = std::counted_iterator(std::move(ifirst), count); auto ret = ranges::uninitialized_move(iter, std::default_sentinel, ofirst, olast); return {std::move(ret.in).base(), ret.out}; } }; inline constexpr uninitialized_move_n_fn uninitialized_move_n{}; |
[編輯] 示例
#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[]{ "No", "Diagnostic", "Required", }; 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_n(std::begin(in), sz, 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: "No" "Diagnostic" "Required" after move, in: "" "" "" after move, out: "No" "Diagnostic" "Required"
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3870 | C++20 | 此演算法可能在 const 儲存上建立物件 | 保持不允許 |
[編輯] 參閱
(C++20) |
將一系列物件移動到未初始化記憶體區域 (演算法函式物件) |
(C++17) |
將多個物件移動到未初始化記憶體區域 (函式模板) |