Namespaces
Variants

std::ranges:: uninitialized_fill

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 < 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) 未初期化メモリ領域 [ first , last ) value をコピーする。以下のように動作する:

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

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

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:

目次

パラメータ

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

戻り値

上記の通り。

計算量

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

例外

宛先範囲内の要素の構築中にスローされる例外。

注記

実装は、出力範囲の値型が TrivialType である場合、 ranges::fill を使用するなどして ranges::uninitialized_fill の効率を改善することができる。

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

実装例

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 (...)
        {   
            // ロールバック: 構築済み要素を破棄
            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{};

#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 ▄▀▄▀▄▀▄▀

不具合報告

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

DR 適用対象 公開時の動作 正しい動作
LWG 3870 C++20 このアルゴリズムは const ストレージ上でオブジェクトを生成する可能性がある 許可されないまま維持

関連項目

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