名前空間
変種
操作

std::ranges::uninitialized_fill

From cppreference.com
< cpp‎ | memory
 
 
メモリ管理ライブラリ
(説明用*)
未初期化メモリのアルゴリズム
(C++17)
(C++17)
(C++17)
制約付き未初期化
メモリアルゴリズム
ranges::uninitialized_fill
(C++20)
Cライブラリ

アロケータ
メモリリソース
ガベージコレクションのサポート
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
未初期化ストレージ
(C++20まで*)
(C++20まで*)
明示的な生存期間管理
 
ヘッダ <memory> で定義
呼び出しシグネチャ
template< no-throw-forward-iterator I, no-throw-sentinel-for<I> S,

          class T >
    requires std::constructible_from<std::iter_value_t<I>, const T&>

I uninitialized_fill( I first, S last, const T& value );
(1) (C++20以降)
(C++26 以降 constexpr)
template< no-throw-forward-range R, class T >

    requires std::constructible_from<ranges::range_value_t<R>,
                                     const T&>
ranges::borrowed_iterator_t<R> uninitialized_fill( R&& r,

                                                   const T& value );
(2) (C++20以降)
(C++26 以降 constexpr)
1) 初期化されていないメモリ領域に `value` をコピーします。範囲は `[first, last)` です。

for (; first != last; ++first)
    ::new (voidify(*first)) std::remove_reference_t<std::iter_reference_t<I>>(value);
return first;

初期化中に例外がスローされた場合、すでに構築されたオブジェクトは未指定の順序で破棄されます。
2) `ranges::uninitialized_fill(ranges::begin(r), ranges::end(r), value)` と同等です。

このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、

目次

[編集] Parameters

first, last - 初期化する要素の範囲を定義するイテレータとセンチネルのペア
r - 初期化する要素の範囲
value - 要素を構築する値

[編集] Return value

上記の通り。

[編集] Complexity

初期化されていないメモリ領域のサイズに対して線形。

[編集] Exceptions

変換先範囲の要素の構築時にスローされる例外。

[編集] Notes

出力範囲の値型がTrivialTypeの場合、実装は `ranges::uninitialized_fill` の効率を改善する可能性があります。例えば、`ranges::fill` を使用するなど。

機能テストマクロ 規格 機能
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr は、特殊なメモリアルゴリズムの場合 (1,2)

[編集] Possible implementation

struct uninitialized_fill_fn
{
    template<no-throw-forward-iterator I, no-throw-sentinel-for<I> S, class T>
        requires std::constructible_from<std::iter_value_t<I>, const T&>
    constexpr I operator()(I first, S last, const T& value) const
    {
        I rollback{first};
        try
        {
            for (; !(first == last); ++first)
                ranges::construct_at(std::addressof(*first), value);
            return first;
        }
        catch (...)
        {   
            // rollback: destroy constructed elements
            for (; rollback != first; ++rollback)
                ranges::destroy_at(std::addressof(*rollback));
            throw;
        }
    }
 
    template<no-throw-forward-range R, class T>
        requires std::constructible_from<ranges::range_value_t<R>, const T&>
    constexpr ranges::borrowed_iterator_t<R> operator()(R&& r, const T& value) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value);
    }
};
 
inline constexpr uninitialized_fill_fn uninitialized_fill{};

[編集] Example

#include <iostream>
#include <memory>
#include <string>
 
int main()
{
    constexpr int n{4};
    alignas(alignof(std::string)) char out[n * sizeof(std::string)];
 
    try
    {
        auto first{reinterpret_cast<std::string*>(out)};
        auto last{first + n};
        std::ranges::uninitialized_fill(first, last, "▄▀▄▀▄▀▄▀");
 
        int count{1};
        for (auto it{first}; it != last; ++it)
            std::cout << count++ << ' ' << *it << '\n';
 
        std::ranges::destroy(first, last);
    }
    catch(...)
    {
        std::cout << "Exception!\n";
    }
}

出力

1 ▄▀▄▀▄▀▄▀
2 ▄▀▄▀▄▀▄▀
3 ▄▀▄▀▄▀▄▀
4 ▄▀▄▀▄▀▄▀

[編集] Defect reports

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 3870 C++20 このアルゴリズムは、const ストレージ上にオブジェクトを作成する可能性があります。 禁止されたままです。

[編集] See also

開始位置と個数で定義された未初期化メモリ領域にオブジェクトをコピーします
(アルゴリズム関数オブジェクト)[編集]
範囲で定義された未初期化メモリ領域にオブジェクトをコピーします
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)