Namespaces
Variants

std::ranges::uninitialized_move_n, std::ranges::uninitialized_move_n_result

ja.cppreference.net から
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲のアルゴリズム (C++20)
制約付きアルゴリズム、例:ranges::copy、ranges::sort、...
ソートと関連操作
分割操作
(C++11)    

ソート操作
二分探索操作
(分割範囲に対して)
集合操作(ソート済み範囲に対して)
マージ操作(ソート済み範囲に対して)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書比較操作
順列操作


 
 
ヘッダ <memory>
で定義呼び出しシグネチャ
template< std::input_iterator I,
          /*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S >
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_rvalue_reference_t<I>>
uninitialized_move_n_result<I, O>
    uninitialized_move_n( I ifirst, std::iter_difference_t<I> count,
                          O ofirst, S olast );
(1) (C++20 から)
(constexpr C++26 から)
template< /*execution-policy*/ Ep, std::random_access_iterator I,
          /*nothrow-random-access-iterator*/ O,
          /*nothrow-sized-sentinel-for*/<O> S >
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_rvalue_reference_t<I>>
uninitialized_move_n_result<I, O>
    uninitialized_move_n( Ep&& policy, I ifirst,
                          std::iter_difference_t<I> count,
                          O ofirst, S olast );
(2) (C++26 から)
ヘルパー型
template< class I, class O >
using uninitialized_move_n_result = ranges::in_out_result<I, O>;
(3) (C++20 から)

/*execution-policy*/の定義については このページを参照してください。その他の説明専用コンセプトの定義については このページを参照してください。

1) コピー先範囲 [ofirst, olast) の要素を、コピー元範囲 ifirst + [0, count) の要素から構築します。あたかも次のように行います。

auto ret = ranges::uninitialized_move(std::counted_iterator(std::move(ifirst), count),
                                      std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};

初期化中に例外が投げられた場合、[ofirst, olast) に既に構築されたオブジェクトは未規定の順序で破棄されます。また、ifirst から始まる入力範囲内の、既にムーブされたオブジェクトは、有効だが未規定の状態のままになります。
[ofirst, olast) が ifirst + [0, count) と重なる場合、動作は未定義です。
2) (1) と同じですが、policy に従って実行されます。

このページで説明されている関数のようなエンティティは アルゴリズム関数オブジェクト (非公式に niebloids として知られています) であり、次のことを意味します:

  • それらのいずれかを呼び出す際に、明示的なテンプレート実引数リストを指定することはできません。
  • それらのいずれも 引数依存の名前検索 で可視ではありません。
  • それらのいずれかが 通常の非修飾名検索 によって関数呼び出し演算子の左側の名前として見つかった場合、 引数依存の名前検索 は抑制されます。

引数

ifirst - 移動元の入力範囲の先頭
ofirst, olast - 初期化する要素の出力範囲を定義するイテレータ-センチネルのペア
count - 移動する要素の数
policy - 使用する実行ポリシー

戻り値

上記の通り。

例外

宛先範囲の要素の構築中にスローされた例外。

2) 実行中:
  • 並列化に必要な一時メモリリソースが利用できない場合、std::bad_alloc がスローされます。
  • アルゴリズム引数を介してオブジェクトにアクセス中にキャッチされない例外がスローされた場合、動作は実行ポリシーによって決定されます( 標準ポリシー、 std::terminate が呼び出されます)。

注記

実装は、ranges::uninitialized_move_n (例えば、ranges::copy_n) 出力範囲の値型が TrivialType である場合。

機能テスト マクロ 値 標準 機能
__cpp_lib_parallel_algorithm 202506L (C++26) 並列範囲アルゴリズム
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr のための 特殊化された <memory> アルゴリズム, (1)

実装例

struct uninitialized_move_n_fn
{
    template<std::input_iterator I,
             /*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S>
        requires std::constructible_from<std::iter_value_t<O>,
                                         std::iter_rvalue_reference_t<I>>
    constexpr ranges::uninitialized_move_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_move(iter, std::default_sentinel, ofirst, olast);
        return {std::move(ret.in).base(), ret.out};
    }
};

inline constexpr uninitialized_move_n_fn uninitialized_move_n{};

例

#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[]{ "No", "Diagnostic", "Required", };
    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_n(std::begin(in), sz, 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: "No" "Diagnostic" "Required"
after move, in: "" "" ""
after move, out: "No" "Diagnostic" "Required"

関連項目

オブジェクトの範囲を未初期化メモリ領域に移動する
(アルゴリズム関数オブジェクト)
指定個数のオブジェクトを未初期化メモリ領域に移動する
(関数テンプレート)