namespace std {
template <class ForwardIterator, class T>
void
uninitialized_fill(ForwardIterator first,
ForwardIterator last,
const T& x); // (1) C++98
template <class NoThrowForwardIterator,
class T = typename iterator_traits<NoThrowForwardIterator>::value_type>
constexpr void
uninitialized_fill(NoThrowForwardIterator first,
NoThrowForwardIterator last,
const T& x); // (1) C++26
template <class ExecutionPolicy, class ForwardIterator, class T>
void
uninitialized_fill(ExecutionPolicy&& exec,
ForwardIterator first,
ForwardIterator last,
const T& x); // (2) C++17
template <class ExecutionPolicy,
class NoThrowForwardIterator,
class T = typename iterator_traits<NoThrowForwardIterator>::value_type>
void
uninitialized_fill(ExecutionPolicy&& exec,
NoThrowForwardIterator first,
NoThrowForwardIterator last,
const T& x); // (2) C++26
}
ę¦č¦
ęŖåęåé åć®ēÆå²ććęå®ćććå¤ć§é
ē½®newććć
ęŖåęåå
„åć¤ćć¬ć¼ćæēÆå²[first, last)ćåęåćć¤ć¤å¤xć§åććć
å¹ę
-
C++98 : 仄äøćØēä¾”
for (; first != last; ++first) ::new (static_cast<void*>(&*first)) typename iterator_traits<ForwardIterator>::value_type(x); -
C++17 : 仄äøćØēä¾”
for (; first != last; ++first) ::new (static_cast<void*>(addressof(*first))) typename iterator_traits<ForwardIterator>::value_type(x);
ę»ćå¤
ćŖć
ä¾å¤
å¼ć³åŗćć³ć³ć¹ćć©ćÆćæćŖć©ććä¾å¤ćéåŗćććå “åććć®ä¾å¤ććć®é¢ę°ć®å¤å“ć«ä¼ęćććåć«ććć®ęē¹ć§ę§ēÆęøć®ćŖććøć§ćÆććÆå Øć¦ęŖč¦å®ć®é åŗć§ē “ę£ććććććŖćć”ćä¾å¤ćéåŗćććå “åćÆåęå対豔é åćÆęŖåęåć®ć¾ć¾ćØćŖćć
åč
- (1), (2) :
- C++26 : å¼ę°ćØćć¦ę³¢ć«ćć³åęå
{}ćåćä»ćć
std::uninitialized_fill(p, p + size, {a, b});
- C++26 : å¼ę°ćØćć¦ę³¢ć«ćć³åęå
ä¾
åŗę¬ēćŖä½æćę¹
#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ćåęåćć¤ć¤ćå¤2ć§åćć
std::uninitialized_fill(p, p + size, 2);
// pć®é åćåęåććććć¤ēÆå²pć®å
Øć¦ć®č¦ē“ ć2ć§åćććć¦ććć確čŖ
std::for_each(p, p + size, [](int x) {
std::cout << x << std::endl;
});
// č¦ē“ ćē “ę£
for (std::size_t i = 0; i < size; ++i) {
std::destroy_at(p + i);
}
// ć”ć¢ćŖč§£ę¾
alloc.deallocate(p, size);
}
åŗå
2
2
2
ę³¢ć«ćć³åęåćå „åćØćć¦ä½æēØćć (C++26)
#include <iostream>
#include <memory>
struct Point {
int x;
int y;
};
int main()
{
std::allocator<Point> alloc;
const std::size_t size = 3;
Point* p = alloc.allocate(size);
// ę³¢ć«ćć³åęåćē“ę„ęø”ćć
std::uninitialized_fill(p, p + size, {1, 2});
for (std::size_t i = 0; i < size; ++i) {
std::cout << p[i].x << "," << p[i].y << std::endl;
}
for (std::size_t i = 0; i < size; ++i) {
std::destroy_at(p + i);
}
alloc.deallocate(p, size);
}
åŗå
1,2
1,2
1,2
é¢é£é ē®
åē §
- LWG Issue 2433
uninitialized_copy()/etc. should tolerate overloadedoperator& - P3508R0 Wording for "constexpr for specialized memory algorithms"
- C++26ćć
constexprćć¤ćć
- C++26ćć
- P3787R2 Adjoints to "Enabling list-initialization for algorithms": uninitialized_fill
- C++26ć§å¤åćć¤ćć¬ć¼ćæććęØč«ććććę”å¼µćććę³¢ć«ćć³åęåćē“ę„ęø”ććććć«ćŖć£ć
- LWG Issue 866. Qualification of placement new-expressions
- C++11ć§ć
uninitialized_fillćč¦ē“ ćę§ēÆććéć«ä½æēØććé ē½®newć::newćØäæ®é£¾ćććć修飾ććŖććØćč¦ē“ åććÆć©ć¹ć¹ć³ć¼ćć®operator newććŖć¼ćć¼ćć¼ććć¦ććå “åć«ćć”ććéøć°ćć¦ćć¾ćć対åæććoperator deletećå¼ć°ććäøę“åćčµ·ćććć
- C++11ć§ć