Namespaces
Variants

std::ranges:: uninitialized_default_construct

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 >

requires std:: default_initializable < std:: iter_value_t < I >>

I uninitialized_default_construct ( I first, S last ) ;
(1) (C++20以降)
(C++26以降constexpr)
template < no-throw-forward-range R >

requires std:: default_initializable < ranges:: range_value_t < R >>
ranges:: borrowed_iterator_t < R >

uninitialized_default_construct ( R && r ) ;
(2) (C++20以降)
(C++26以降constexpr)
1) std:: iter_value_t < I > のオブジェクトを未初期化メモリ領域 [ first , last ) デフォルト初期化 によって構築する。以下のように動作する:

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

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

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

目次

パラメータ

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

戻り値

上記の通り。

計算量

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

例外

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

注記

実装は、 std:: iter_value_t < I > オブジェクトをデフォルト初期化する際に非自明なデフォルトコンストラクタが呼び出されない場合(観測可能な効果を変更せずに)、オブジェクトの構築を省略することができます。これは std::is_trivially_default_constructible によって検出可能です。

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

実装例

struct uninitialized_default_construct_fn
{
    template<no-throw-forward-iterator I, no-throw-sentinel-for<I> S>
        requires std::default_initializable<std::iter_value_t<I>>
    constexpr I operator()(I first, S last) const
    {
        using ValueType = std::remove_reference_t<std::iter_reference_t<I>>;
        if constexpr (std::is_trivially_default_constructible_v<ValueType>)
            return ranges::next(first, last); // 初期化をスキップ
        I rollback{first};
        try
        {
            for (; !(first == last); ++first)
                ::new (static_cast<void*>(std::addressof(*first))) ValueType;
            return first;
        }
        catch (...) // ロールバック: 構築済み要素を破棄
        {
            for (; rollback != first; ++rollback)
                ranges::destroy_at(std::addressof(*rollback));
            throw;
        }
    }
    template<no-throw-forward-range R>
        requires std::default_initializable<ranges::range_value_t<R>>
    constexpr ranges::borrowed_iterator_t<R> operator()(R&& r) const
    {
        return (*this)(ranges::begin(r), ranges::end(r));
    }
};
inline constexpr uninitialized_default_construct_fn uninitialized_default_construct{};

#include <cstring>
#include <iostream>
#include <memory>
#include <string>
int main()
{
    struct S { std::string m{"▄▀▄▀▄▀▄▀"}; };
    constexpr int n{4};
    alignas(alignof(S)) char out[n * sizeof(S)];
    try
    {
        auto first{reinterpret_cast<S*>(out)};
        auto last{first + n};
        std::ranges::uninitialized_default_construct(first, last);
        auto count{1};
        for (auto it{first}; it != last; ++it)
            std::cout << count++ << ' ' << it->m << '\n';
        std::ranges::destroy(first, last);
    }
    catch (...) { std::cout << "Exception!\n"; }
    // 注意: "trivial types" の場合、uninitialized_default_construct は
    // 一般的に指定された未初期化メモリ領域をゼロフィルしません。
    constexpr char sample[]{'A', 'B', 'C', 'D', '\n'};
    char v[]{'A', 'B', 'C', 'D', '\n'};
    std::ranges::uninitialized_default_construct(std::begin(v), std::end(v));
    if (std::memcmp(v, sample, sizeof(v)) == 0)
    {
        std::cout << "  ";
        // 未定義動作の可能性あり、CWG 1997 の解決待ち:
        // for (const char c : v) { std::cout << c << ' '; }
        for (const char c : sample)
            std::cout << c << ' ';
    }
    else
        std::cout << "Unspecified\n";
}

出力例:

1 ▄▀▄▀▄▀▄▀
2 ▄▀▄▀▄▀▄▀
3 ▄▀▄▀▄▀▄▀
4 ▄▀▄▀▄▀▄▀
  A B C D

不具合報告

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

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

関連項目

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