std::ranges:: uninitialized_copy_n, std::ranges:: uninitialized_copy_n_result
|
ヘッダーで定義
<memory>
|
||
|
呼び出しシグネチャ
|
||
|
template
<
std::
input_iterator
I,
no-throw-input-iterator
O, no
-
throw
-
sentinel
-
for
<
O
>
S
>
|
(1) |
(C++20以降)
(C++26以降constexpr) |
|
ヘルパー型
|
||
|
template
<
class
I,
class
O
>
using uninitialized_copy_n_result = ranges:: in_out_result < I, O > ; |
(2) | (C++20以降) |
N を ranges:: min ( count, ranges:: distance ( ofirst, olast ) ) とする。
N
個の要素を
ifirst
から始まる範囲から未初期化メモリ領域
[
ofirst
,
olast
)
へ、以下のようにコピーする
auto
ret
=
ranges::
uninitialized_copy
(
std::
counted_iterator
(
std
::
move
(
ifirst
)
, count
)
,
std::
default_sentinel
, ofirst, olast
)
;
return
{
std
::
move
(
ret.
in
)
.
base
(
)
, ret.
out
}
;
初期化中に例外がスローされた場合、既に構築されたオブジェクトは未規定の順序で破棄されます。
[
ofirst
,
olast
)
が
ifirst
+
[
0
,
count
)
と重なる場合、動作は未定義です。
このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:
- 明示的なテンプレート引数リストは、いずれかを呼び出す際に指定できません。
- いずれも 実引数依存の名前探索 では可視になりません。
- いずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって見つかった場合、 実引数依存の名前探索 は抑制されます。
目次 |
パラメータ
| ifirst | - | コピー元要素の 範囲 の先頭 |
| count | - | コピーする要素数 |
| ofirst, olast | - | コピー先要素の 範囲 を定義するイテレータ-番兵ペア |
戻り値
上記の通り。
計算量
𝓞(N) .
例外
宛先範囲内の要素の構築中にスローされる例外。
注記
実装は、出力範囲の値型が
TrivialType
である場合、
ranges::copy_n
などを使用することで、
ranges::uninitialized_copy_n
の効率を改善することができる。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr 対応の 特殊化メモリアルゴリズム , ( 1 ) |
実装例
struct uninitialized_copy_n_fn { template<std::input_iterator I, no-throw-input-iterator O, no-throw-sentinel-for<O> S> requires std::constructible_from<std::iter_value_t<O>, std::iter_reference_t<I>> constexpr ranges::uninitialized_copy_n_result<I, O> operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const { auto iter = std::counted_iterator(std::move(ifirst), count); auto ret = ranges::uninitialized_copy(iter, std::default_sentinel, ofirst, olast); return {std::move(ret.in).base(), ret.out}; } }; inline constexpr uninitialized_copy_n_fn uninitialized_copy_n{}; |
例
#include <iomanip> #include <iostream> #include <memory> #include <string> int main() { const char* stars[]{"Procyon", "Spica", "Pollux", "Deneb", "Polaris"}; constexpr int n{4}; alignas(alignof(std::string)) char out[n * sizeof(std::string)]; try { auto first{reinterpret_cast<std::string*>(out)}; auto last{first + n}; auto ret{std::ranges::uninitialized_copy_n(std::begin(stars), n, first, last)}; std::cout << '{'; for (auto it{first}; it != ret.out; ++it) std::cout << (it == first ? "" : ", ") << std::quoted(*it); std::cout << "};\n"; std::ranges::destroy(first, last); } catch (...) { std::cout << "uninitialized_copy_n exception\n"; } }
出力:
{"Procyon", "Spica", "Pollux", "Deneb"};
関連項目
|
(C++20)
|
オブジェクトの範囲を未初期化メモリ領域にコピーする
(アルゴリズム関数オブジェクト) |
|
(C++11)
|
指定された数のオブジェクトを未初期化メモリ領域にコピーする
(関数テンプレート) |