std::uninitialized_move_n
定義於標頭檔案 <memory> |
||
template< class InputIt, class Size, class NoThrowForwardIt > std::pair<InputIt, NoThrowForwardIt> |
(1) | (C++17 起) (C++26 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class Size, class NoThrowForwardIt > |
(2) | (C++17 起) |
+
[
0,
count)
複製元素(如果支援,使用移動語義)到一個從 d_first 開始的未初始化記憶體區域,如同透過
for (; count > 0; ++d_first, (void) ++first, --count)
::new (voidify
(*d_first))
typename std::iterator_traits<NoThrowForwardIt>::value_type(/* value */);
return {first, d_first};
+
[
0,
count)
中的一些物件會處於有效但未指定的狀態,並且已構造的物件將以未指定的順序銷燬。
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 起) |
如果 d_first |
(C++20 起) |
目錄 |
[編輯] 引數
first | - | 要移動元素的範圍的起始 |
d_first | - | 目標範圍的開頭 |
count | - | 要移動元素的數量 |
policy | - | 要使用的 執行策略 |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-NoThrowForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-透過 `NoThrowForwardIt` 的有效例項,不允許遞增、賦值、比較或間接引用丟擲異常。 |
[編輯] 返回值
如上所述。
[編輯] 複雜度
關於 count 的線性複雜度。
[編輯] 異常
帶有名為 ExecutionPolicy
的模板引數的過載會按如下方式報告錯誤:
- 如果作為演算法一部分呼叫的函式執行丟擲異常,並且
ExecutionPolicy
是 標準策略 之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 注意
當輸入迭代器解引用為右值時,std::uninitialized_move_n
的行為與 std::uninitialized_copy_n 相同。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 對於未初始化記憶體演算法,(1) |
[編輯] 可能的實現
template<class InputIt, class Size, class NoThrowForwardIt> constexpr std::pair<InputIt, NoThrowForwardIt> uninitialized_move_n(InputIt first, Size count, NoThrowForwardIt d_first) { using ValueType = typename std::iterator_traits<NoThrowForwardIt>::value_type; NoThrowForwardIt current = d_first; try { for (; count > 0; ++first, (void) ++current, --count) { auto addr = static_cast<void*>(std::addressof(*current)); if constexpr (std::is_lvalue_reference_v<decltype(*first)>) ::new (addr) ValueType(std::move(*first)); else ::new (addr) ValueType(*first); } } catch (...) { std::destroy(d_first, current); throw; } return {first, current}; } |
[編輯] 示例
#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[]{"One", "Definition", "Rule"}; 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::uninitialized_move_n(std::begin(in), sz, first); print("after move, in: ", std::begin(in), std::end(in)); print("after move, out: ", first, last); std::destroy(first, last); } catch (...) { std::cout << "Exception!\n"; } std::free(out); } }
可能的輸出
initially, in: "One" "Definition" "Rule" after move, in: "" "" "" after move, out: "One" "Definition" "Rule"
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3870 | C++20 | 此演算法可能在 const 儲存上建立物件 | 保持不允許 |
LWG 3918 | C++17 | 需要額外的臨時具現化 當輸入迭代器解引用為純右值時 |
在這種情況下複製元素 |
[編輯] 參閱
(C++17) |
將一系列物件移動到未初始化記憶體區域 (函式模板) |
(C++11) |
將一定數量的物件複製到未初始化記憶體區域 (函式模板) |
(C++20) |
將多個物件移動到未初始化記憶體區域 (演算法函式物件) |