std:: uninitialized_copy_n
|
ヘッダーで定義
<memory>
|
||
|
template
<
class
InputIt,
class
Size,
class
NoThrowForwardIt
>
NoThrowForwardIt uninitialized_copy_n
(
InputIt first, Size count,
|
(1) |
(C++11以降)
(C++26以降 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
Size,
class
NoThrowForwardIt
>
|
(2) | (C++17以降) |
for
(
;
count
>
0
;
++
d_first,
(
void
)
++
first,
--
count
)
::
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 | - | コピーする要素範囲の先頭 |
| count | - | コピーする要素の数 |
| d_first | - | コピー先範囲の先頭 |
| policy | - | 使用する 実行ポリシー |
| 型要件 | ||
-
InputIt
は
LegacyInputIterator
の要件を満たさなければならない
|
||
-
ForwardIt
は
LegacyForwardIterator
の要件を満たさなければならない
|
||
-
NoThrowForwardIt
は
LegacyForwardIterator
の要件を満たさなければならない
|
||
-
NoThrowForwardIt
の有効なインスタンスを通じたインクリメント、代入、比較、間接参照は例外をスローしてはならない
|
||
戻り値
コピーされた最後の要素の次の要素を指すイテレータ。
計算量
count に対して線形。
例外
ExecutionPolicy
という名前のテンプレートパラメータを持つオーバーロードは、
以下のようにエラーを報告します:
-
アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、
ExecutionPolicyが 標準ポリシー のいずれかであるときは、 std::terminate が呼び出されます。それ以外のExecutionPolicyについては、動作は実装定義です。 - アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされます。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr 対応の 特殊化メモリアルゴリズム , ( 1 ) |
実装例
template<class InputIt, class Size, class NoThrowForwardIt> constexpr NoThrowForwardIt uninitialized_copy_n(InputIt first, Size count, NoThrowForwardIt d_first) { using T = typename std::iterator_traits<NoThrowForwardIt>::value_type; NoThrowForwardIt current = d_first; try { for (; count > 0; ++first, (void) ++current, --count) ::new (static_cast<void*>(std::addressof(*current))) T(*first); } catch (...) { for (; d_first != current; ++d_first) d_first->~T(); throw; } return current; } |
`タグ内のC++コードは翻訳対象外 - C++固有の用語(関数名、キーワード、型名など)は翻訳せず保持 翻訳すべき自然言語テキストが含まれていないため、HTML構造とC++コードはそのまま保持されています。
例
#include <algorithm> #include <iostream> #include <memory> #include <string> #include <tuple> #include <vector> int main() { std::vector<std::string> v = {"This", "is", "an", "example"}; std::string* p; std::size_t sz; std::tie(p, sz) = std::get_temporary_buffer<std::string>(v.size()); sz = std::min(sz, v.size()); std::uninitialized_copy_n(v.begin(), sz, p); for (std::string* i = p; i != p + sz; ++i) { std::cout << *i << ' '; i->~basic_string<char>(); } std::cout << '\n'; std::return_temporary_buffer(p); }
出力例:
This is an example
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2133 | C++98 |
効果の説明で
for
ループの反復式に
++ d_first, ++ first, -- count を使用しており、 これにより引数依存の operator, ルックアップが発生する |
一方のオペランドの値を
破棄してこれらの ADLを無効化する |
| LWG 2433 | C++11 |
このアルゴリズムはオーバーロードされた
operator
&
によって
ハイジャックされる可能性がある |
std::addressof を使用する |
| LWG 3870 | C++20 |
このアルゴリズムは
const
ストレージ上に
オブジェクトを作成する可能性がある |
許可されないまま維持 |
関連項目
|
オブジェクトの範囲を未初期化メモリ領域にコピーする
(関数テンプレート) |
|
|
(C++20)
|
指定された数のオブジェクトを未初期化メモリ領域にコピーする
(アルゴリズム関数オブジェクト) |