std:: uninitialized_default_construct
|
ヘッダーで定義
<memory>
|
||
|
template
<
class
NoThrowForwardIt
>
void
uninitialized_default_construct
(
NoThrowForwardIt first,
|
(1) |
(C++17以降)
(constexpr C++26以降) |
|
template
<
class
ExecutionPolicy,
class
NoThrowForwardIt
>
void
uninitialized_default_construct
(
ExecutionPolicy
&&
policy,
|
(2) | (C++17以降) |
[
first
,
last
)
において、
typename
std::
iterator_traits
<
NoThrowForwardIt
>
::
value_type
型のオブジェクトを
デフォルト初期化
によって構築する。以下のように振る舞う:
for
(
;
first
!
=
last
;
++
first
)
::
new
(
voidify
(
*
first
)
)
typename
std::
iterator_traits
<
NoThrowForwardIt
>
::
value_type
;
|
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, last | - | 初期化する要素の範囲を定義するイテレータのペア |
| policy | - | 使用する実行ポリシー |
| 型要件 | ||
-
NoThrowForwardIt
は
LegacyForwardIterator
の要件を満たさなければならない。
|
||
-
NoThrowForwardIt
の有効なインスタンスを通じたインクリメント、代入、比較、間接参照は例外をスローしてはならない。
|
||
計算量
first と last の間の距離に対して線形。
例外
テンプレートパラメータ
ExecutionPolicy
を持つオーバーロードは、
以下のようにエラーを報告します:
-
アルゴリズムの一部として呼び出された関数の実行が例外をスローした場合、
ExecutionPolicyが 標準ポリシー のいずれかであるとき、 std::terminate が呼び出される。その他のExecutionPolicyについては、動作は実装定義である。 - アルゴリズムがメモリの確保に失敗した場合、 std::bad_alloc がスローされる。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr 対応の 特殊化メモリアルゴリズム , ( 1 ) |
実装例
template<class NoThrowForwardIt> constexpr void uninitialized_default_construct(NoThrowForwardIt first, NoThrowForwardIt last) { using Value = typename std::iterator_traits<NoThrowForwardIt>::value_type; NoThrowForwardIt current = first; try { for (; current != last; ++current) { ::new (static_cast<void*>(std::addressof(*current))) Value; } } catch (...) { std::destroy(first, current); throw; } } |
例
#include <cstring> #include <iostream> #include <memory> #include <string> struct S { std::string m{"Default value"}; }; int main() { constexpr int n{3}; alignas(alignof(S)) unsigned char mem[n * sizeof(S)]; try { auto first{reinterpret_cast<S*>(mem)}; auto last{first + n}; std::uninitialized_default_construct(first, last); for (auto it{first}; it != last; ++it) std::cout << it->m << '\n'; std::destroy(first, last); } catch (...) { std::cout << "Exception!\n"; } // スカラー型の場合、uninitialized_default_constructは // 一般的に指定された未初期化メモリ領域をゼロフィルしません。 int v[]{1, 2, 3, 4}; const int original[]{1, 2, 3, 4}; std::uninitialized_default_construct(std::begin(v), std::end(v)); // 未定義動作の可能性あり、CWG 1997の解決待ち // for (const int i : v) // std::cout << i << ' '; // 結果は未規定 std::cout << (std::memcmp(v, original, sizeof(v)) == 0 ? "Unmodified\n" : "Modified\n"); }
出力例:
Default value Default value Default value Unmodified
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3870 | C++20 | このアルゴリズムは const ストレージ上にオブジェクトを作成する可能性がある | 禁止されたまま維持 |
関連項目
|
開始位置と個数で定義される未初期化メモリ領域で
default-initialization
によってオブジェクトを構築する
(関数テンプレート) |
|
|
(C++17)
|
範囲で定義される未初期化メモリ領域で
value-initialization
によってオブジェクトを構築する
(関数テンプレート) |
|
範囲で定義される未初期化メモリ領域で
default-initialization
によってオブジェクトを構築する
(アルゴリズム関数オブジェクト) |