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 がスローされます。
[編集] 注記
入力イテレータが rvalue をデリファレンスする場合、std::uninitialized_move_n の動作は std::uninitialized_copy_n と同じです。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | 特殊なメモリアルゴリズムの場合、(1) は constexpr です。 |
[編集] 実装例
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++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 3870 | C++20 | このアルゴリズムは、const ストレージ上にオブジェクトを作成する可能性があります。 | 禁止されたままです。 |
| LWG 3918 | C++17 | 追加の一時的なマテリアライゼーションが必要でした 入力イテレータが prvalue をデリファレンスする場合 |
この場合、要素がコピーされます |
[編集] 関連項目
| (C++17) |
オブジェクトの範囲を未初期化メモリ領域にムーブします (関数テンプレート) |
| (C++11) |
指定された数のオブジェクトを未初期化メモリ領域にコピーします (関数テンプレート) |
| (C++20) |
指定された数のオブジェクトを未初期化メモリ領域にムーブします (アルゴリズム関数オブジェクト) |