Namespaces
Variants

std:: uninitialized_move

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
ヘッダーで定義 <memory>
template < class InputIt, class NoThrowForwardIt >

NoThrowForwardIt uninitialized_move ( InputIt first, InputIt last,

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

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

NoThrowForwardIt d_first ) ;
(2) (C++17以降)
1) [ first , last ) の要素を(move semanticsがサポートされている場合はそれを使用して)未初期化メモリ領域の d_first から始まる位置にコピーする。動作は以下のコードと同等:

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

where /* value */ is std :: move ( * first ) if * first is of an lvalue reference type, or * first otherwise.
初期化中に例外がスローされた場合、 [ first , last ) 内の一部のオブジェクトは有効だが未規定の状態に残され、既に構築されたオブジェクトは未規定の順序で破棄されます。
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 + [ 0 , std:: distance ( first, last ) ) [ first , last ) と重複する場合、動作は未定義です。

(C++20以降)

目次

パラメータ

first, last - 移動する要素の範囲を定義するイテレータのペア
d_first - 宛先範囲の先頭
policy - 使用する実行ポリシー
型要件
-
InputIt LegacyInputIterator の要件を満たさなければならない
-
ForwardIt LegacyForwardIterator の要件を満たさなければならない
-
NoThrowForwardIt LegacyForwardIterator の要件を満たさなければならない
-
NoThrowForwardIt の有効なインスタンスを通じたインクリメント、代入、比較、間接参照は例外をスローしてはならない

戻り値

上記の通り。

計算量

first last の間の距離に対して線形。

例外

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

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

注記

入力イテレータが右辺値にデリファレンスする場合、 std::uninitialized_move の動作は std::uninitialized_copy と同じです。

機能テスト マクロ 標準 機能
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 対応 特殊化メモリアルゴリズム , ( 1 )

実装例

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をデリファレンスする場合に
追加の一時マテリアライゼーションが必要だった
この場合には要素をコピーする

関連項目

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