名前空間
変種
操作

std::uninitialized_move

From cppreference.com
< cpp‎ | memory
 
 
メモリ管理ライブラリ
(説明用*)
未初期化メモリのアルゴリズム
uninitialized_move
(C++17)
(C++17)
(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 NoThrowForwardIt >

NoThrowForwardIt uninitialized_move( InputIt first, InputIt last,

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

          class ForwardIt, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_move( ExecutionPolicy&& policy,
                                     ForwardIt first, ForwardIt last,

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

for (; first != last; ++d_first, (void) ++first)
    ::new (voidify(*d_first))
        typename std::iterator_traits<NoThrowForwardIt>::value_type(/* value */);
return d_first;

ここで、/* value */ は、*first が左辺値参照型の場合は std::move(*first)、それ以外の場合は *first です。
初期化中に例外がスローされた場合、[firstlast) 内の一部のオブジェクトは有効だが未指定の状態になり、すでに構築されたオブジェクトは未指定の順序で破棄されます。
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 + [0std::distance(first, last))[firstlast) と重なる場合、動作は未定義です。

(C++20以降)

目次

[編集] パラメータ

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

[編集] 戻り値

上記の通り。

[編集] 計算量

firstlast の距離に対して線形。

[編集] 例外

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

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

[編集] 備考

入力イテレータが右辺値を逆参照する場合、`std::uninitialized_move` の動作は std::uninitialized_copy と同じです。

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

[編集] 可能な実装

template<class InputIt, class NoThrowForwardIt>
constexpr NoThrowForwardIt uninitialized_move(InputIt first, InputIt last,
                                              NoThrowForwardIt d_first)
{
    using ValueType = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    auto current = d_first;
    try
    {
        for (; first != last; ++first, (void) ++current)
        {
            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);
        }
        return current;
    }
    catch (...)
    {
        std::destroy(d_first, current);
        throw;
    }
}

[編集]

#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", "Work!"};
    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(std::begin(in), std::end(in), 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: "Home" "Work!"
after move, in: "" ""
after move, out: "Home" "Work!"

[編集] 欠陥レポート

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

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

[編集] 関連項目

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