Namespaces
Variants

std:: uninitialized_copy

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_copy ( InputIt first, InputIt last,

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

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

NoThrowForwardIt d_first ) ;
(2) (since C++17)
1) 範囲 [ first , last ) から未初期化メモリ領域(先頭 d_first )へ、以下のように要素をコピーする

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

初期化中に例外がスローされた場合、既に構築されたオブジェクトは未規定の順序で破棄されます。
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 の有効なインスタンスに対するインクリメント、代入、比較、間接参照は例外を投げてはならない NoThrowForwardIt 値に & * を適用すると、その値型へのポインタが得られなければならない (C++11まで)

戻り値

コピーされた最後の要素の次の要素を指すイテレータ。

計算量

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

例外

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

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

注記

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

実装例

template<class InputIt, class NoThrowForwardIt>
constexpr NoThrowForwardIt uninitialized_copy(InputIt first, InputIt last,
                                              NoThrowForwardIt d_first)
{
    using T = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = d_first;
    try
    {
        for (; first != last; ++first, (void) ++current)
            ::new (static_cast<void*>(std::addressof(*current))) T(*first);
        return current;
    }
    catch (...)
    {
        for (; d_first != current; ++d_first)
            d_first->~T();
        throw;
    }
}
このC++コードは`uninitialized_copy`関数の実装を示しています。HTMLタグ、属性、および` `ブロック内のC++コードはすべて元のまま保持されています。

#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
int main()
{
    const char *v[] = {"This", "is", "an", "example"};
    auto sz = std::size(v);
    if (void *pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz))
    {
        try
        {
            auto first = static_cast<std::string*>(pbuf);
            auto last = std::uninitialized_copy(std::begin(v), std::end(v), first);
            for (auto it = first; it != last; ++it)
                std::cout << *it << '_';
            std::cout << '\n';
            std::destroy(first, last);
        }
        catch (...) {}
        std::free(pbuf);
    }
}

出力:

This_is_an_example_

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 866 C++98 NoThrowForwardIt の値型を T とした場合、
T::operator new が存在するとプログラムが不適格になる可能性がある
代わりにグローバルな置換newを
使用する
LWG 2133 C++98 効果の説明で反復式 ++d_first, ++first を持つ
for ループを使用しており、これにより
operator, の実引数依存の名前探索が行われる
一方のオペランドの値を
破棄してそのADLを
無効化する
LWG 2433 C++11 このアルゴリズムはオーバーロードされた operator& によって
ハイジャックされる可能性がある
std::addressof を使用する
LWG 3870 C++20 このアルゴリズムは const ストレージ上に
オブジェクトを作成する可能性がある
許可されないまま維持

関連項目

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