namespace std::ranges {
template <no-throw-forward-iterator I,
no-throw-sentinel<I> S,
class T>
requires constructible_from<iter_value_t<I>, const T&>
I uninitialized_fill(I first, S last, const T& x); // (1) C++20
template <no-throw-forward-iterator I,
no-throw-sentinel<I> S,
class T = iter_value_t<I>>
requires constructible_from<iter_value_t<I>, const T&>
constexpr I
uninitialized_fill(I first, S last, const T& x); // (1) C++26
template <no-throw-forward-range R,
class T>
requires constructible_from<range_value_t<R>, const T&>
borrowed_iterator_t<R>
uninitialized_fill(R&& r, const T& x); // (2) C++20
template <no-throw-forward-range R,
class T = range_value_t<R>>
requires constructible_from<range_value_t<R>, const T&>
constexpr borrowed_iterator_t<R>
uninitialized_fill(R&& r, const T& x); // (2) C++26
template <execution-policy Ep,
random_access_iterator I,
sized_sentinel_for<I> S,
class T = iter_value_t<I>>
requires constructible_from<iter_value_t<I>, const T&>
I uninitialized_fill(Ep&& exec,
I first, S last, const T& x); // (3) C++26
template <execution-policy Ep,
sized-random-access-range R,
class T = range_value_t<R>>
requires constructible_from<range_value_t<R>, const T&>
borrowed_iterator_t<R>
uninitialized_fill(Ep&& exec,
R&& r, const T& x); // (4) C++26
}
概要
未初期化領域の範囲 (r、[first, last)) を、指定された値で配置newで初期化する。
- (1): イテレータ範囲を指定する
- (2): Rangeを直接指定する
- (3): (1)の並列アルゴリズム版。実行ポリシーを指定する
- (4): (2)の並列アルゴリズム版。実行ポリシーを指定する
テンプレートパラメータ制約
- (1):
Iがno-throw-forward-iteratorであるSがIに対する例外を投げない番兵であるIの要素型が、const T&型を引数として構築可能である
- (2):
Rがno-throw-forward-rangeであるRの要素型が、const T&型を引数として構築可能である
効果
説明用の関数voidifyがあるとして、
template<class T>
constexpr void* voidify(T& obj) noexcept {
return const_cast<void*>(static_cast<const volatile void*>(addressof(obj)));
}
以下と等価である:
for (; first != last; ++first) {
::new (voidify(*first)) remove_reference_t<iter_reference_t<I>>(x);
}
return first;
例外
呼び出すコンストラクタなどから例外が送出された場合、その例外がこの関数の外側に伝播される前に、その時点で構築済のオブジェクトは全て未規定の順序で破棄される。すなわち、例外が送出された場合は初期化対象領域は未初期化のままとなる。
備考
- C++26では、テンプレートパラメータ
Tにデフォルトテンプレート引数(iter_value_t<I>またはrange_value_t<R>)を指定するオーバーロードが追加された。これにより、波カッコ初期化リスト ({...}) を入力値xとして渡せるようになる。波カッコ初期化リストは型を持たないため、Tがデフォルト引数として要素型から推論されることで、この関数に波カッコ初期化リストを直接渡せる。
例
基本的な使い方
#include <iostream>
#include <memory>
#include <algorithm>
int main()
{
std::allocator<int> alloc;
// メモリ確保。
// この段階では、[p, p + size)の領域は未初期化
const std::size_t size = 3;
int* p = alloc.allocate(size);
// 未初期化領域[p, p + size)を初期化しつつ、値2で埋める
std::ranges::uninitialized_fill(std::ranges::subrange{p, p + size}, 2);
// pの領域が初期化され、かつ範囲pの全ての要素が2で埋められているか確認
std::for_each(p, p + size, [](int x) {
std::cout << x << std::endl;
});
// 要素を破棄
std::ranges::destroy(p, p + size);
// メモリ解放
alloc.deallocate(p, size);
}
出力
2
2
2
波カッコ初期化を入力として使用する (C++26)
#include <iostream>
#include <memory>
#include <ranges>
struct Point {
int x;
int y;
};
int main()
{
std::allocator<Point> alloc;
const std::size_t size = 3;
Point* p = alloc.allocate(size);
// 波カッコ初期化リストでPoint{1, 2}を直接渡せる
std::ranges::uninitialized_fill(
std::ranges::subrange{p, p + size},
{1, 2});
for (std::size_t i = 0; i < size; ++i) {
std::cout << '(' << p[i].x << ", " << p[i].y << ')' << std::endl;
}
std::ranges::destroy(p, p + size);
alloc.deallocate(p, size);
}
出力
(1, 2)
(1, 2)
(1, 2)
並列アルゴリズムの例 (C++26)
#include <iostream>
#include <memory>
#include <execution>
int main() {
std::allocator<int> alloc;
int* p = alloc.allocate(3);
// 並列に未初期化領域を42で埋める
std::ranges::uninitialized_fill(
std::execution::par, p, p + 3, 42);
for (int i = 0; i < 3; ++i) {
std::cout << p[i] << ' ';
}
std::cout << std::endl;
alloc.deallocate(p, 3);
}
出力
42 42 42
バージョン
言語
- C++20
処理系
- Clang: 16.0 ✅
- GCC: 10.2.0 ✅
- Visual C++: 2019 Update 10 ✅
関連項目
参照
- P0896R4 The One Ranges Proposal
- P3508R0 Wording for "constexpr for specialized memory algorithms"
- C++26から
constexprがついた
- C++26から
- P3179R9 C++ parallel range algorithms
- P3787R2 Adjoints to "Enabling list-initialization for algorithms":
uninitialized_fill- C++26から波カッコ初期化リストを入力として使用できるよう、要素型をデフォルトテンプレート引数とするオーバーロードが追加された