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