std::uninitialized_copy
提供: cppreference.com
ヘッダ <memory> で定義
|
||
template< class InputIt, class ForwardIt > ForwardIt uninitialized_copy( InputIt first, InputIt last, ForwardIt d_first ); |
(1) | |
template< class ExecutionPolicy, class InputIt, class ForwardIt > ForwardIt uninitialized_copy( ExecutionPolicy&& policy, InputIt first, InputIt last, ForwardIt d_first ); |
(2) | (C++17以上) |
1) 以下のように行われたかのように、範囲
[first, last)
の要素を d_first
から始まる未初期化メモリ領域にコピーします。
for (; first != last; ++d_first, (void) ++first) ::new (static_cast<void*>(std::addressof(*d_first))) typename std::iterator_traits<ForwardIt>::value_type(*first);
初期化中に例外が投げられた場合、すでに構築されたオブジェクトは未規定の順序で破棄されます。
2) (1) と同じですが、
policy
に従って実行されます。 このオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。目次 |
[編集] 引数
first, last | - | コピーする要素の範囲 |
d_first | - | コピー先の範囲の先頭 |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-ForwardIt の有効なインスタンスのインクリメント、代入、比較、間接参照は例外を投げてはなりません。
|
[編集] 戻り値
コピーした最後の要素の次の要素を指すイテレータ。
[編集] 計算量
first
と last
の距離に比例。
[編集] 例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集] 実装例
template<class InputIt, class ForwardIt> ForwardIt uninitialized_copy(InputIt first, InputIt last, ForwardIt d_first) { typedef typename std::iterator_traits<ForwardIt>::value_type Value; ForwardIt current = d_first; try { for (; first != last; ++first, (void) ++current) { ::new (static_cast<void*>(std::addressof(*current))) Value(*first); } return current; } catch (...) { for (; d_first != current; ++d_first) { d_first->~Value(); } throw; } } |
[編集] 例
Run this code
#include <iostream> #include <memory> #include <cstdlib> #include <string> int main() { const char *v[] = {"This", "is", "an", "example"}; auto sz = std::size(v); if(void *pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz)) { try { auto first = static_cast<std::string*>(pbuf); auto last = std::uninitialized_copy(std::begin(v), std::end(v), first); for (auto it = first; it != last; ++it) std::cout << *it << '_'; std::destroy(first, last); } catch(...) {} std::free(pbuf); } }
出力:
This_is_an_example_
[編集] 関連項目
(C++11) |
指定個数のオブジェクトをメモリの未初期化領域にコピーします (関数テンプレート) |