Namespaces
Variants

std::uninitialized_value_construct_n

- ja.cppreference.net
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとレンジに関するアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy、ranges::sort、...
ソートと関連操作
分割操作
(C++11)    

ソート操作
二分探索操作
(分割された範囲に対して)
集合操作 (ソート済み範囲に対して)
マージ操作 (ソート済み範囲に対して)
ヒープ操作
最小値/最大値操作
(C++11)
(C++17)
辞書順比較操作
順列操作


 
 
ヘッダ <memory>
template< class NoThrowForwardIt, class Size >
NoThrowForwardIt uninitialized_value_construct_n
    ( NoThrowForwardIt first, Size count );
で定義 (1) (C++17以上)
(C++26からconstexpr)
template< class ExecutionPolicy, class NoThrowForwardIt, class Size >
NoThrowForwardIt uninitialized_value_construct_n
    ( ExecutionPolicy&& policy, NoThrowForwardIt first, Size count );
(2) (C++17以上)
1) コピー先範囲 first + [0, count) の要素を 値初期化 によって構築します。以下と同様の効果を持ちます。

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

初期化中に例外が送出された場合、既に構築されたオブジェクトは未規定の順序で破棄されます。
2) (1) と同じですが、policy に従って実行されます。
このオーバーロードは、以下の式の値が true である場合にのみオーバーロード解決に参加します。

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20まで)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20以上)

パラメータ

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

戻り値

上記の通り。

例外

2) 実行処理中:
  • 並列化に必要な一時的なメモリリソースが利用できない場合、std::bad_alloc が送出される。
  • アルゴリズムの引数を介してオブジェクトにアクセスしているときに捕捉されない例外が送出された場合、その動作は実行ポリシーによって決定される(標準ポリシーに対しては、std::terminate が呼び出される)。

注記

機能テストマクロ 値 標準 機能
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用 特殊化された<memory>アルゴリズム、(1)

実装例

template<class NoThrowForwardIt, class Size>
constexpr ForwardIt uninitialized_value_construct_n(NoThrowForwardIt first, Size count)
{
    using T = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = first;
    try
    {
        for (; countn > 0; (void) ++current, --count)
            ::new (static_cast<void*>(std::addressof(*current))) T();
        return current;
    }
    catch (...)
    {
        std::destroy(first, current);
        throw;
    }
}

例

#include <iostream>
#include <memory>
#include <string>
int main()
{
    struct S { std::string m{"Default value"}; };
    constexpr int n{3};
    alignas(alignof(S)) unsigned char mem[n * sizeof(S)];
    try
    {
        auto first{reinterpret_cast<S*>(mem)};
        auto last = std::uninitialized_value_construct_n(first, n);
        for (auto it{first}; it != last; ++it)
            std::cout << it->m << '\n';
        std::destroy(first, last);
    }
    catch (...)
    {
        std::cout << "Exception!\n";
    }
    // スカラ型の場合、uninitialized_value_construct_nは
    // 指定された未初期化メモリ領域をゼロ初期化します
    int v[]{1, 2, 3, 4};
    for (const int i : v)
        std::cout << i << ' ';
    std::cout << '\n';
    std::uninitialized_value_construct_n(std::begin(v), std::size(v));
    for (const int i : v)
        std::cout << i << ' ';
    std::cout << '\n';
}

出力:

Default value
Default value
Default value
1 2 3 4
0 0 0 0

関連項目

未初期化のメモリ領域内の範囲でオブジェクトを value-initialization によって構築する
(関数テンプレート)
未初期化のメモリ領域内で、開始位置とカウントで定義された範囲のオブジェクトを default-initialization によって構築する
(関数テンプレート)
未初期化のメモリ領域内で、開始位置とカウントで定義された範囲のオブジェクトを value-initialization によって構築する
(アルゴリズム関数オブジェクト)