std:: uninitialized_move
|
ヘッダーで定義
<memory>
|
||
|
template
<
class
InputIt,
class
NoThrowForwardIt
>
NoThrowForwardIt uninitialized_move
(
InputIt first, InputIt last,
|
(1) |
(C++17以降)
(constexpr since C++26) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
NoThrowForwardIt
>
|
(2) | (C++17以降) |
[
first
,
last
)
の要素を(move semanticsがサポートされている場合はそれを使用して)未初期化メモリ領域の
d_first
から始まる位置にコピーする。動作は以下のコードと同等:
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) | constexpr 対応 特殊化メモリアルゴリズム , ( 1 ) |
実装例
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)
|
未初期化メモリ領域にオブジェクトの範囲を移動する
(アルゴリズム関数オブジェクト) |