Namespaces
Variants

std:: uninitialized_fill_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 NoThrowForwardIt, class Size, class T >

NoThrowForwardIt uninitialized_fill_n ( NoThrowForwardIt first,

Size count, const T & value ) ;
(1) (C++26以降 constexpr)
template < class ExecutionPolicy,

class NoThrowForwardIt, class Size, class T >
NoThrowForwardIt uninitialized_fill_n ( ExecutionPolicy && policy,
NoThrowForwardIt first,

Size count, const T & value ) ;
(2) (C++17以降)
1) 未初期化メモリ領域 first + [ 0 , count ) value をコピーする。以下のように動作する:

for ( ; count -- ; ++ first )
:: new ( voidify ( * first ) )
typename std:: iterator_traits < NoThrowForwardIt > :: value_type ( value ) ;
return 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以降)

目次

パラメータ

first - 初期化する要素範囲の先頭
count - 構築する要素の数
value - 要素を構築するための値
型要件
-
NoThrowForwardIt LegacyForwardIterator の要件を満たさなければならない。
-
NoThrowForwardIt の有効なインスタンスを通じたインクリメント、代入、比較、間接参照は例外を投げてはならない。 NoThrowForwardIt 値に & * を適用すると、その値型へのポインタが得られなければならない。 (C++11まで)

戻り値

上記の通り。

計算量

count に対して線形。

例外

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

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

注記

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

実装例

template<class NoThrowForwardIt, class Size, class T>
constexpr NoThrowForwardIt uninitialized_fill_n(NoThrowForwardIt first,
                                                Size count, const T& value)
{
    using V = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = first;
    try
    {
        for (; count > 0; ++current, (void) --count)
            ::new (static_cast<void*>(std::addressof(*current))) V(value);
        return current;
    }
    catch (...)
    {
        for (; first != current; ++first)
            first->~V();
        throw;
    }
    return current;
}

#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
int main()
{
    std::string* p;
    std::size_t sz;
    std::tie(p, sz) = std::get_temporary_buffer<std::string>(4);
    std::uninitialized_fill_n(p, sz, "Example");
    for (std::string* i = p; i != p + sz; ++i)
    {
        std::cout << *i << '\n';
        i->~basic_string<char>();
    }
    std::return_temporary_buffer(p);
}

出力:

Example
Example
Example
Example

不具合報告

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

DR 適用対象 公開時の動作 正しい動作
LWG 866 C++98 T NoThrowForwardIt の値型とした場合、
T :: operator new が存在すると、プログラムが不適格となる可能性があった
グローバル配置 new を使用する
LWG 1339 C++98 埋め込み範囲に続く最初の要素の位置が
返されなかった
返される
LWG 2433 C++11 このアルゴリズムはオーバーロードされた operator & によってハイジャックされる可能性があった std::addressof を使用する
LWG 3870 C++20 このアルゴリズムは const ストレージ上にオブジェクトを作成する可能性があった 許可されないまま維持

関連項目

未初期化メモリ領域(範囲で定義される)にオブジェクトをコピーする
(関数テンプレート)
未初期化メモリ領域(開始位置とカウントで定義される)にオブジェクトをコピーする
(アルゴリズム関数オブジェクト)