std::uninitialized_move
| ヘッダ <memory> で定義 |
||
template< class InputIt, class NoThrowForwardIt > NoThrowForwardIt uninitialized_move( InputIt first, InputIt last, |
(1) | (C++17以降) (C++26 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class NoThrowForwardIt > |
(2) | (C++17以降) |
[first, last) の要素を (サポートされていればムーブセマンティクスを使用して) 次のようにコピーします。
for (; first != last; ++d_first, (void) ++first)
::new (voidify(*d_first))
typename std::iterator_traits<NoThrowForwardIt>::value_type(/* value */);
return d_first;
[first, last) 内の一部のオブジェクトは有効だが未指定の状態になり、すでに構築されたオブジェクトは未指定の順序で破棄されます。|
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, last | - | 移動する要素の範囲を定義するイテレータの組 |
| d_first | - | コピー先範囲の先頭 |
| policy | - | 使用する 実行ポリシー |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-NoThrowForwardIt は LegacyForwardIterator の要件を満たす必要があります。 | ||
-有効な NoThrowForwardIt のインスタンスを介したインクリメント、代入、比較、間接参照は例外をスローしてはなりません。 | ||
[編集] 戻り値
上記の通り。
[編集] 計算量
first と last の距離に対して線形。
[編集] 例外
テンプレートパラメータ ExecutionPolicy を持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[編集] 備考
入力イテレータが右辺値を逆参照する場合、`std::uninitialized_move` の動作は std::uninitialized_copy と同じです。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | 特殊なメモリアルゴリズムの場合、(1) は constexpr です。 |
[編集] 可能な実装
template<class InputIt, class NoThrowForwardIt> constexpr NoThrowForwardIt uninitialized_move(InputIt first, InputIt last, NoThrowForwardIt d_first) { using ValueType = typename std::iterator_traits<NoThrowForwardIt>::value_type; auto current = d_first; try { for (; first != last; ++first, (void) ++current) { 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); } return current; } catch (...) { std::destroy(d_first, current); throw; } } |
[編集] 例
#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", "Work!"}; 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(std::begin(in), std::end(in), 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: "Home" "Work!" after move, in: "" "" after move, out: "Home" "Work!"
[編集] 欠陥レポート
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 3870 | C++20 | このアルゴリズムは、const ストレージ上にオブジェクトを作成する可能性があります。 | 禁止されたままです。 |
| LWG 3918 | C++17 | 追加の一時的な具体化が必要でした 入力イテレータがprvalueを逆参照する場合 |
この場合は要素をコピーします |
[編集] 関連項目
| オブジェクトの範囲を未初期化メモリ領域にコピーします (関数テンプレート) | |
| (C++17) |
指定された数のオブジェクトを未初期化メモリ領域にムーブします (関数テンプレート) |
| (C++20) |
オブジェクトの範囲を未初期化メモリ領域にムーブします (アルゴリズム関数オブジェクト) |