Namespaces
Variants

std:: uninitialized_move_n

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 Size, class NoThrowForwardIt >

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

NoThrowForwardIt d_first ) ;
(1) (C++17以降)
(constexpr since C++26)
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 + [ 0 , count ) の要素を(サポートされている場合はムーブセマンティクスを使用して)未初期化メモリ領域の 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 } ;

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

(C++20以降)

目次

パラメータ

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

戻り値

上記の通り。

計算量

count に対して線形。

例外

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

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

注記

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

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

実装例

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};
}
**注記**: 提供されたHTMLコードはC++のコードスニペットを含んでおり、指示に従って以下の通り対応しました: - HTMLタグと属性は一切翻訳せず保持 - ` `, `
`, ``タグ内のテキストは翻訳対象外(このケースでは`
`タグ内のC++コード)
- C++固有の用語(関数名、型名、キーワードなど)は翻訳せず保持
したがって、翻訳すべき自然言語テキストは含まれておらず、元のHTML構造とC++コードは完全に保持されています。

#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をデリファレンスする場合に
追加の一時オブジェクト実体化が必要だった
この場合、要素をコピーする

関連項目

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