名前空間
変種
操作

std::uninitialized_move_n

From cppreference.com
< cpp‎ | memory
 
 
メモリ管理ライブラリ
(説明用*)
未初期化メモリのアルゴリズム
(C++17)
(C++17)
uninitialized_move_n
(C++17)
(C++17)
制約付き未初期化
メモリアルゴリズム
Cライブラリ

アロケータ
メモリリソース
ガベージコレクションのサポート
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
未初期化ストレージ
(C++20まで*)
(C++20まで*)
明示的な生存期間管理
 
ヘッダ <memory> で定義
template< class InputIt, class Size, class NoThrowForwardIt >

std::pair<InputIt, NoThrowForwardIt>
    uninitialized_move_n( InputIt first, Size count,

                          NoThrowForwardIt d_first );
(1) (C++17以降)
(C++26 以降 constexpr)
template< class ExecutionPolicy,

          class ForwardIt, class Size, class NoThrowForwardIt >
std::pair<ForwardIt, NoThrowForwardIt>
    uninitialized_move_n( ExecutionPolicy&& policy, ForwardIt first,

                          Size count, NoThrowForwardIt d_first );
(2) (C++17以降)
1) first + [0count) の範囲の要素を、サポートされていればムーブセマンティクスを使用して、d_first で始まる未初期化メモリ領域に、あたかも次のようにコピーします。

for (; count > 0; ++d_first, (void) ++first, --count)
    ::new (voidify(*d_first))
        typename std::iterator_traits<NoThrowForwardIt>::value_type(/* value */);
return {first, d_first};

ここで /* value */ は、*first が lvalue 参照型の場合 std::move(*first)、そうでない場合は *first です。
初期化中に例外がスローされた場合、first + [0count) の一部のオブジェクトは有効ですが未指定の状態になり、すでに構築されたオブジェクトは未指定の順序で破棄されます。
2) (1) と同じですが、policy に従って実行されます。
このオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加します。

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true です。

(C++20まで)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true です。

(C++20以降)


d_first + [0count)first + [0count) が重複する場合、動作は未定義です。

(C++20以降)

目次

[編集] パラメータ

first - 移動する要素の範囲の開始
d_first - コピー先範囲の先頭
count - 移動する要素の数
policy - 使用する 実行ポリシー
型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。
-
NoThrowForwardItLegacyForwardIterator の要件を満たす必要があります。
-
有効な NoThrowForwardIt のインスタンスを介したインクリメント、代入、比較、間接参照は例外をスローしてはなりません。

[編集] 戻り値

上記の通り。

[編集] 計算量

count に対して線形。

[編集] 例外

テンプレートパラメータ ExecutionPolicy を持つオーバーロードは、次のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外をスローし、ExecutionPolicy標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他の ExecutionPolicy の場合、動作は実装定義です。
  • アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。

[編集] 注記

入力イテレータが rvalue をデリファレンスする場合、std::uninitialized_move_n の動作は std::uninitialized_copy_n と同じです。

機能テストマクロ 規格 機能
__cpp_lib_raw_memory_algorithms 202411L (C++26) 特殊なメモリアルゴリズムの場合、(1)constexpr です。

[編集] 実装例

template<class InputIt, class Size, class NoThrowForwardIt>
constexpr std::pair<InputIt, NoThrowForwardIt>
    uninitialized_move_n(InputIt first, Size count, NoThrowForwardIt d_first)
{
    using ValueType = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = d_first;
    try
    {
        for (; count > 0; ++first, (void) ++current, --count) {
            auto addr = static_cast<void*>(std::addressof(*current));
            if constexpr (std::is_lvalue_reference_v<decltype(*first)>)
                ::new (addr) ValueType(std::move(*first));
            else
                ::new (addr) ValueType(*first);
        }
    }
    catch (...)
    {
        std::destroy(d_first, current);
        throw;
    }
    return {first, current};
}

[編集]

#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[]{"One", "Definition", "Rule"};
    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::uninitialized_move_n(std::begin(in), sz, first);
 
            print("after move, in: ", std::begin(in), std::end(in));
            print("after move, out: ", first, last);
 
            std::destroy(first, last);
        }
        catch (...)
        {
            std::cout << "Exception!\n";
        }
        std::free(out);
    }
}

実行結果の例

initially, in: "One" "Definition" "Rule" 
after move, in: "" "" "" 
after move, out: "One" "Definition" "Rule"

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 3870 C++20 このアルゴリズムは、const ストレージ上にオブジェクトを作成する可能性があります。 禁止されたままです。
LWG 3918 C++17 追加の一時的なマテリアライゼーションが必要でした
入力イテレータが prvalue をデリファレンスする場合
この場合、要素がコピーされます

[編集] 関連項目

オブジェクトの範囲を未初期化メモリ領域にムーブします
(関数テンプレート) [編集]
指定された数のオブジェクトを未初期化メモリ領域にコピーします
(関数テンプレート) [編集]
指定された数のオブジェクトを未初期化メモリ領域にムーブします
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)