std::ranges::uninitialized_copy, std::ranges::uninitialized_copy_result
ja.cppreference.net より
| ヘッダ <memory>
|
||
| で定義 |
||
template< std::input_iterator I, std::sentinel_for<I> S1,
/*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S2 >
requires std::constructible_from<std::iter_value_t<O>,
std::iter_reference_t<I>>
uninitialized_copy_result<I, O>
uninitialized_copy( I ifirst, S1 ilast, O ofirst, S2 olast );
|
呼び出しシグネチャ | (1) (C++20 以上) |
template< ranges::input_range IR, /*nothrow-forward-range*/ OR >
requires std::constructible_from<ranges::range_value_t<OR>,
ranges::range_reference_t<IR>>
uninitialized_copy_result<ranges::borrowed_iterator_t<IR>,
ranges::borrowed_iterator_t<OR>>
uninitialized_copy( IR&& in_range, OR&& out_range );
|
(C++26 より constexpr) | (2) (C++20 以上) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I, std::sized_sentinel_for<I> S1,
/*nothrow-random-access-iterator*/ O,
/*nothrow-sized-sentinel-for*/<O> S2 >
requires std::constructible_from<std::iter_value_t<O>,
std::iter_reference_t<I>>
uninitialized_copy_result<I, O>
uninitialized_copy( Ep&& policy, I ifirst, S1 ilast, O ofirst, S2 olast );
|
(C++26 より constexpr) | (3) |
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ IR,
/*nothrow-sized-random-access-range*/ OR >
requires std::constructible_from<ranges::range_value_t<OR>,
ranges::range_reference_t<IR>>
uninitialized_copy_result<ranges::borrowed_iterator_t<IR>,
ranges::borrowed_iterator_t<OR>>
uninitialized_copy( Ep&& policy, IR&& in_range, OR&& out_range );
|
(C++26 以上) | (4) |
| (C++26 以上) |
||
template< class I, class O >
using uninitialized_copy_result = ranges::in_out_result<I, O>;
|
ヘルパー型 | (5) |
(C++20 以上)/*execution-policy*/の定義についてはこのページを、/*sized-random-access-range*/の定義についてはこのページを、その他の説明専用コンセプトの定義についてはこのページを参照してください。
1) コピー元範囲
[ofirst, olast) の要素からコピー先範囲 [ifirst, ilast) に要素を構築します。以下と同様の動作です。
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) と重なっている場合、動作は未定義です。2) (1) と同じですが、コピー元範囲として
in_range を、コピー先範囲として out_range を使用します。3,4) (1,2) と同じですが、
policy に従って実行されます。このページに記載されている関数のようなエンティティは アルゴリズム関数オブジェクト (非公式に niebloid として知られています) です。つまり:
- それらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- それらのいずれも 引数依存の名前探索では可視ではありません。
- 通常の非修飾名探索によって関数呼び出し演算子の左側の名前として見つかった場合、引数依存の名前探索は抑制されます。
引数
| ifirst, ilast | - | コピー元の要素を定義するイテレータと番兵のペア(ソース範囲) |
| in_range | - | コピー元の要素の範囲 |
| ofirst, olast | - | コピー先の要素を定義するイテレータと番兵のペア(デスティネーション範囲) |
| out_range | - | コピー先の |
| 範囲 | policy | - 実行ポリシー |
戻り値
上記の通り。
例外
コピー先の範囲内の要素の構築中に投げられる例外。
3,4) 実行処理中:
- 並列化に必要な一時メモリリソースが利用できない場合、std::bad_alloc が投げられる。
- アルゴリズムの引数を介してオブジェクトにアクセスしているときに捕捉されない例外が投げられた場合、動作は実行ポリシーによって決まる(標準ポリシーの場合、std::terminate が呼び出される)。
注記
出力範囲の値型がranges::uninitialized_copyTrivialTypeである場合、実装は効率を向上させることができます。
| 機能テストマクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | 並列範囲アルゴリズム |
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 特殊化された<memory>アルゴリズム向け、(1,2)
|
実装例
struct uninitialized_copy_fn
{
template<std::input_iterator I, std::sentinel_for<I> S1,
/*nothrow-forward-iterator*/ O, /*nothrow-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 (...) // rollback: destroy constructed elements
{
for (; ofirst != current; ++ofirst)
ranges::destroy_at(std::addressof(*ofirst));
throw;
}
}
template<ranges::input_range IR, /*nothrow-forward-range*/ OR>
requires std::constructible_from<ranges::range_value_t<OR>,
ranges::range_reference_t<IR>>
constexpr 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::next(ranges::begin(out_range), ranges::end(out_range)));
}
template<ranges::forward_range IR, /*nothrow-forward-range*/ OR>
requires std::constructible_from<ranges::range_value_t<OR>,
ranges::range_reference_t<IR>>
constexpr 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::next(ranges::begin(in_range), ranges::end(in_range)),
ranges::begin(out_range),
ranges::next(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++20) |
指定された数のオブジェクトを初期化されていないメモリ領域にコピーする (アルゴリズム関数オブジェクト) |
| オブジェクトの範囲を初期化されていないメモリ領域にコピーする (関数テンプレート) |