std::ranges:: uninitialized_move, std::ranges:: uninitialized_move_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
|
(2) |
(C++20以降)
(constexpr since C++26) |
|
ヘルパー型
|
||
|
template
<
class
I,
class
O
>
using uninitialized_move_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
)
にコピーする(サポートされている場合はmove semanticsを使用)
for
(
;
ifirst
!
=
ilast
&&
ofirst
!
=
olast
;
++
ofirst,
(
void
)
++
ifirst
)
::
new
(
voidify
(
*
ofirst
)
)
std::
remove_reference_t
<
std::
iter_reference_t
<
O
>>
(
ranges::
iter_move
(
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::copy_n
などを使用することで、
ranges::uninitialized_move
の効率を改善することがあります。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr 対応の 特殊化メモリアルゴリズム , ( 1,2 ) |
実装例
struct uninitialized_move_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_rvalue_reference_t<I>> constexpr ranges::uninitialized_move_result<I, O> operator()(I ifirst, S1 ilast, O ofirst, S2 olast) const { using ValueType = std::remove_reference_t<std::iter_reference_t<O>>; O current{ofirst}; try { for (; !(ifirst == ilast or current == olast); ++ifirst, ++current) ::new (static_cast<void*>(std::addressof(*current)))) ValueType(ranges::iter_move(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>, ranges::range_rvalue_reference_t<IR>> constexpr ranges::uninitialized_move_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_move_fn uninitialized_move{}; |
例
#include <cstdlib> #include <iomanip> #include <iostream> #include <memory> #include <string> void print(auto rem, auto first, auto last) { for (std::cout << rem; first != last; ++first) std::cout << std::quoted(*first) << ' '; std::cout << '\n'; } int main() { std::string in[]{"Home", "World"}; print("initially, in: ", std::begin(in), std::end(in)); if (constexpr auto sz = std::size(in); void* out = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz)) { try { auto first{static_cast<std::string*>(out)}; auto last{first + sz}; std::ranges::uninitialized_move(std::begin(in), std::end(in), first, last); print("after move, in: ", std::begin(in), std::end(in)); print("after move, out: ", first, last); std::ranges::destroy(first, last); } catch (...) { std::cout << "Exception!\n"; } std::free(out); } }
出力例:
initially, in: "Home" "World" after move, in: "" "" after move, out: "Home" "World"
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 3870 | C++20 | このアルゴリズムは const ストレージ上にオブジェクトを作成する可能性がある | 許可されないまま維持 |
関連項目
|
(C++20)
|
指定された数のオブジェクトを未初期化メモリ領域にムーブする
(アルゴリズム関数オブジェクト) |
|
(C++17)
|
オブジェクトの範囲を未初期化メモリ領域にムーブする
(関数テンプレート) |