std::uninitialized_move_n
ja.cppreference.net より
| ヘッダ <memory>
|
||
template< class InputIt, class Size, class NoThrowForwardIt >
std::pair<InputIt, NoThrowForwardIt>
uninitialized_move_n( InputIt first, Size count,
NoThrowForwardIt d_first );
|
で定義 (1) | (C++17以上) (C++26以上でconstexpr) |
template< class ExecutionPolicy,
class ForwardIt, class Size, class NoThrowForwardIt >
std::pair<ForwardIt, NoThrowForwardIt>
uninitialized_move_n( ExecutionPolicy&& policy, ForwardIt first,
Size count, NoThrowForwardIt d_first );
|
(2) | (C++17以上) |
1)
d_first を先頭とするコピー先レンジに、count を先頭とするコピー元レンジの最初の first 個の要素から、あたかも以下のようにして要素を構築する:
for (; count > 0; ++d_first, (void) ++first, --count)
::new (voidify(*d_first))
typename std::iterator_traits<NoThrowForwardIt>::value_type(deref-move(*iter));
return {first, d_first};
初期化中に例外が送出された場合、
first + [0, count) 内の一部のオブジェクトは有効だが未規定の状態のまま残され、既に構築されたオブジェクトは未規定の順序で破棄される。2) (1)と同じだが、
policy に従って実行される。 このオーバーロードは、以下の式の値が
true である場合にのみオーバーロード解決に参加する:
|
|
(C++20まで) |
|
|
(C++20以上) |
|
|
(C++20以上) |
パラメータ
| first | - | 移動する要素の範囲の先頭 |
| d_first | - | 宛先範囲の先頭 |
| count | - | 移動する要素の数 |
| policy | - | 使用する 実行ポリシー |
| 型要件 | ||
-
InputIt
は
LegacyInputIterator
の要件を満たさなければならない
|
||
-
ForwardIt
は
LegacyForwardIterator
の要件を満たさなければならない
|
||
-
NoThrowForwardIt
は
LegacyForwardIterator
の要件を満たさなければならない
|
||
-
NoThrowForwardIt
の有効なインスタンスを通じたインクリメント、代入、比較、間接参照は例外をスローしてはならない
|
||
戻り値
上記の通り。
例外
2) 実行処理中:
- 並列化に必要な一時メモリリソースが利用できない場合、std::bad_allocがスローされます。
- アルゴリズムの引数を介してオブジェクトにアクセス中に捕捉されない例外がスローされた場合、動作は実行ポリシーによって決まります(標準ポリシーの場合、std::terminateが呼び出されます)。
注釈
入力イテレータが右辺値を参照解決する場合、std::uninitialized_move_nの動作はstd::uninitialized_copy_nと同じです。
| 機能テストマクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 用の 特殊化された <memory> アルゴリズム, (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++標準に遡及適用されました。
| DR | 適用先 | 公開時点の動作 | 正しい動作 |
|---|---|---|---|
| LWG 3918 | C++17 | ムーブ構築は常に実行される( 入力イテレータがprvalueにデリファレンスする場合でも) |
ムーブ構築は行われない この場合 |
関連項目
|
(C++17)
|
オブジェクトの範囲を未初期化メモリ領域にムーブする
(関数テンプレート) |
|
(C++11)
|
指定された数のオブジェクトを未初期化メモリ領域にコピーする
(関数テンプレート) |
|
(C++20)
|
指定された数のオブジェクトを未初期化メモリ領域にムーブする
(アルゴリズム関数オブジェクト) |