std:: uninitialized_move_n
|
ヘッダーで定義
<memory>
|
||
|
template
<
class
InputIt,
class
Size,
class
NoThrowForwardIt
>
std::
pair
<
InputIt, NoThrowForwardIt
>
|
(1) |
(C++17以降)
(constexpr since C++26) |
|
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++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 3870 | C++20 | このアルゴリズムは const ストレージ上にオブジェクトを作成する可能性がある | 許可されないまま維持 |
| LWG 3918 | C++17 |
入力イテレータがprvalueをデリファレンスする場合に
追加の一時オブジェクト実体化が必要だった |
この場合、要素をコピーする |
関連項目
|
(C++17)
|
オブジェクトの範囲を未初期化メモリ領域にムーブする
(関数テンプレート) |
|
(C++11)
|
指定された数のオブジェクトを未初期化メモリ領域にコピーする
(関数テンプレート) |
|
(C++20)
|
指定された数のオブジェクトを未初期化メモリ領域にムーブする
(アルゴリズム関数オブジェクト) |