std::uninitialized_fill
cppreference.com
<tbody>
</tbody>
template< class ForwardIt, class T > void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value ); |
(1) | |
template< class ExecutionPolicy, class ForwardIt, class T > void uninitialized_fill( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value ); |
(2) | ( C++17) |
1)
value , [first, last),
for (; first != last; ++first)::new (/*VOIDIFY*/(*first))typename std::iterator_traits<ForwardIt>::value_type(value);
/* VOIDIFY */(e) :
|
( C++11) |
|
( C++11) |
, .
2) , (1),
policy. , |
|
( C++20) |
|
|
( C++20) |
| first, last | ||
| value | , | |
| policy | . . | |
-ForwardIt LegacyForwardIterator.
| ||
- , , ForwardIt . &* ForwardIt . ( C++11)
| ||
()
first last.
ExecutionPolicy :
- , ,
ExecutionPolicy, std::terminate.ExecutionPolicy. - , std::bad_alloc.
template<class ForwardIt, class T>
void uninitialized_fill(ForwardIt first, ForwardIt last, const T& value)
{
using V = typename std::iterator_traits<ForwardIt>::value_type;
ForwardIt current = first;
try
{
for (; current != last; ++current)
::new (static_cast<void*>(std::addressof(*current))) V(value);
}
catch (...)
{
for (; first != current; ++first)
first->~V();
throw;
}
}
|
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
int main()
{
const std::size_t sz = 4;
std::allocator<std::string> alloc;
std::string* p = alloc.allocate(sz);
std::uninitialized_fill(p, p + sz, "");
for (std::string* i = p; i != p + sz; ++i)
{
std::cout << *i << '\n';
i->~basic_string<char>();
}
alloc.deallocate(p, sz);
}
:
C++:
| LWG 866 | C++98 | T ForwardIt, T::operator new , |
-new |
| LWG 2433 | C++11 | operator&
|
std::addressof |
| LWG 3870 | C++20 | const
|
| , ( ) | |
(C++20) |
, () |