[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/memory/uninitialized_fill [Back]  [Original]

std::uninitialized_fill cppreference.com
cppreference.com

std::uninitialized_fill

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
no section name
no section name
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)



no section name
 
<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 , [firstlast),

for (; first != last; ++first)
::new (/*VOIDIFY*/(*first))
typename std::iterator_traits<ForwardIt>::value_type(value);

/* VOIDIFY */(e) :
static_cast<void*>(&e)
( C++11)
static_cast<void*>(std::addressof(e))
( C++11)
, .
2) , (1), policy. ,

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
value ,
policy . .
-
ForwardIt LegacyForwardIterator.
-
, , ForwardIt . &* ForwardIt . ( C++11)

()

first last.

ExecutionPolicy :

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

,
( ) []
,
() []

Web Proxy Viewer  |  New URL  |  Original Page