std:: uninitialized_copy
|
ヘッダーで定義
<memory>
|
||
|
template
<
class
InputIt,
class
NoThrowForwardIt
>
NoThrowForwardIt uninitialized_copy
(
InputIt first, InputIt last,
|
(1) | (constexpr since C++26) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
NoThrowForwardIt
>
|
(2) | (since C++17) |
[
first
,
last
)
から未初期化メモリ領域(先頭
d_first
)へ、以下のように要素をコピーする
for
(
;
first
!
=
last
;
++
d_first,
(
void
)
++
first
)
::
new
(
voidify
(
*
d_first
)
)
typename
std::
iterator_traits
<
NoThrowForwardIt
>
::
value_type
(
*
first
)
;
|
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
の有効なインスタンスに対するインクリメント、代入、比較、間接参照は例外を投げてはならない
NoThrowForwardIt
値に
&
*
を適用すると、その値型へのポインタが得られなければならない
(C++11まで)
|
||
戻り値
コピーされた最後の要素の次の要素を指すイテレータ。
計算量
first と last の間の距離に対して線形。
例外
テンプレートパラメータ
ExecutionPolicy
を持つオーバーロードは、
以下のようにエラーを報告します:
-
アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、
ExecutionPolicyが 標準ポリシー のいずれかであるとき、 std::terminate が呼び出される。その他のExecutionPolicyについては、動作は実装定義である。 - アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされる。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr 対応の 特殊化メモリアルゴリズム , ( 1 ) |
実装例
template<class InputIt, class NoThrowForwardIt> constexpr NoThrowForwardIt uninitialized_copy(InputIt first, InputIt last, NoThrowForwardIt d_first) { using T = typename std::iterator_traits<NoThrowForwardIt>::value_type; NoThrowForwardIt current = d_first; try { for (; first != last; ++first, (void) ++current) ::new (static_cast<void*>(std::addressof(*current))) T(*first); return current; } catch (...) { for (; d_first != current; ++d_first) d_first->~T(); throw; } } |
`ブロック内のC++コードはすべて元のまま保持されています。
例
#include <cstdlib> #include <iostream> #include <memory> #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::cout << '\n'; std::destroy(first, last); } catch (...) {} std::free(pbuf); } }
出力:
This_is_an_example_
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 866 | C++98 |
NoThrowForwardIt
の値型を
T
とした場合、
T::operator new
が存在するとプログラムが不適格になる可能性がある
|
代わりにグローバルな置換newを
使用する |
| LWG 2133 | C++98 |
効果の説明で反復式
++d_first, ++first
を持つ
for
ループを使用しており、これにより
operator,
の実引数依存の名前探索が行われる
|
一方のオペランドの値を
破棄してそのADLを 無効化する |
| LWG 2433 | C++11 |
このアルゴリズムはオーバーロードされた
operator&
によって
ハイジャックされる可能性がある |
std::addressof
を使用する
|
| LWG 3870 | C++20 |
このアルゴリズムは
const
ストレージ上に
オブジェクトを作成する可能性がある |
許可されないまま維持 |
関連項目
|
(C++11)
|
指定された数のオブジェクトを未初期化メモリ領域にコピーする
(関数テンプレート) |
|
(C++20)
|
オブジェクトの範囲を未初期化メモリ領域にコピーする
(アルゴリズム関数オブジェクト) |