std::ranges:: uninitialized_copy, std::ranges:: uninitialized_copy_result
|
定義先ヘッダ
<memory>
|
||
|
呼び出しシグネチャ
|
||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S1,
no-throw-forward-iterator
O, no
-
throw
-
sentinel
-
for
<
O
>
S2
>
|
(1) |
(C++20以降)
(constexpr since C++26) |
|
template
<
ranges::
input_range
IR,
no-throw-forward-range
OR
>
requires
std::
constructible_from
<
ranges::
range_value_t
<
OR
>
,
|
(2) |
(C++20以降)
(constexpr since C++26) |
|
ヘルパー型
|
||
|
template
<
class
I,
class
O
>
using uninitialized_copy_result = ranges:: in_out_result < I, O > ; |
(3) | (C++20以降) |
N を ranges:: min ( ranges:: distance ( ifirst, ilast ) , ranges:: distance ( ofirst, olast ) ) とする。
[
ifirst
,
ilast
)
から未初期化メモリ領域
[
ofirst
,
olast
)
に構築する。以下のように動作する:
for
(
;
ifirst
!
=
ilast
&&
ofirst
!
=
olast
;
++
ofirst,
(
void
)
++
ifirst
)
::
new
(
voidify
(
*
ofirst
)
)
std::
remove_reference_t
<
std::
iter_reference_t
<
O
>>
(
*
ifirst
)
;
return
{
std
::
move
(
ifirst
)
, ofirst
}
;
[
ofirst
,
olast
)
が
[
ifirst
,
ilast
)
と重なる場合、動作は未定義です。
ranges:: begin ( out_range ) , ranges:: end ( out_range ) ) ; 。
このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:
- 明示的なテンプレート引数リストは、いずれかを呼び出す際に指定することはできません。
- いずれも 実引数依存の名前探索 では可視になりません。
- いずれかが関数呼び出し演算子の左側の名前として 通常の非修飾名前探索 によって見つかった場合、 実引数依存の名前探索 は抑制されます。
目次 |
パラメータ
| ifirst, ilast | - | コピー元の要素を定義するイテレータ-センチネルペア 範囲 |
| in_range | - |
コピー元の要素の
range
|
| ofirst, olast | - | 要素のコピー先を定義するイテレータ-センチネルペア 範囲 |
| out_range | - |
コピー先の
range
|
戻り値
上記の通り。
計算量
𝓞(N) .
例外
宛先範囲内の要素の構築中にスローされる例外。
注記
実装は、出力範囲の値型が
TrivialType
である場合、
ranges::uninitialized_copy
の効率を改善することができます。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr 対応の 特殊化メモリアルゴリズム , ( 1,2 ) |
実装例
struct uninitialized_copy_fn { template<std::input_iterator I, std::sentinel_for<I> S1, no-throw-forward-iterator O, no-throw-sentinel-for<O> S2> requires std::constructible_from<std::iter_value_t<O>, std::iter_reference_t<I>> constexpr ranges::uninitialized_copy_result<I, O> operator()(I ifirst, S1 ilast, O ofirst, S2 olast) const { O current{ofirst}; try { for (; !(ifirst == ilast or current == olast); ++ifirst, ++current) ranges::construct_at(std::addressof(*current), *ifirst); return {std::move(ifirst), std::move(current)}; } catch (...) // ロールバック:構築済み要素を破棄 { for (; ofirst != current; ++ofirst) ranges::destroy_at(std::addressof(*ofirst)); throw; } } template<ranges::input_range IR, no-throw-forward-range OR> requires std::constructible_from<ranges::range_value_t<OR>, constexpr ranges::range_reference_t<IR>> ranges::uninitialized_copy_result<ranges::borrowed_iterator_t<IR>, ranges::borrowed_iterator_t<OR>> operator()(IR&& in_range, OR&& out_range) const { return (*this)(ranges::begin(in_range), ranges::end(in_range), ranges::begin(out_range), ranges::end(out_range)); } }; inline constexpr uninitialized_copy_fn uninitialized_copy{}; |
例
#include <cstdlib> #include <iomanip> #include <iostream> #include <memory> #include <string> int main() { const char* v[]{"This", "is", "an", "example"}; if (const auto sz{std::size(v)}; void* pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz)) { try { auto first{static_cast<std::string*>(pbuf)}; auto last{first + sz}; std::ranges::uninitialized_copy(std::begin(v), std::end(v), first, last); std::cout << "{"; for (auto it{first}; it != last; ++it) std::cout << (it == first ? "" : ", ") << std::quoted(*it); std::cout << "};\n"; std::ranges::destroy(first, last); } catch (...) { std::cout << "uninitialized_copy exception\n"; } std::free(pbuf); } }
出力:
{"This", "is", "an", "example"};
不具合報告
以下の動作変更欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 3870 | C++20 | このアルゴリズムは const ストレージ上にオブジェクトを作成する可能性がある | 許可されないまま維持 |
関連項目
|
(C++20)
|
指定された数のオブジェクトを未初期化メモリ領域にコピーする
(アルゴリズム関数オブジェクト) |
|
オブジェクトの範囲を未初期化メモリ領域にコピーする
(関数テンプレート) |