Namespaces
Variants

std::ranges:: uninitialized_fill

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
헤더에 정의됨 <memory>
호출 시그니처
template < no-throw-forward-iterator I, no - throw - sentinel - for < I > S,

class T >
requires std:: constructible_from < std:: iter_value_t < I > , const T & >

I uninitialized_fill ( I first, S last, const T & value ) ;
(1) (C++20부터)
(C++26부터 constexpr)
template < no-throw-forward-range R, class T >

requires std:: constructible_from < ranges:: range_value_t < R > ,
const T & >
ranges:: borrowed_iterator_t < R > uninitialized_fill ( R && r,

const T & value ) ;
(2) (C++20부터)
(C++26부터 constexpr)
1) value 를 초기화되지 않은 메모리 영역 [ first , last ) 에 다음과 같이 복사합니다

for ( ; first ! = last ; ++ first )
:: new ( voidify ( * first ) ) std:: remove_reference_t < std:: iter_reference_t < I >> ( value ) ;
return first ;

예외가 초기화 과정에서 발생하면, 이미 생성된 객체들은 지정되지 않은 순서로 파괴됩니다.
2) 다음과 동일함: return ranges :: uninitialized_fill ( ranges:: begin ( r ) , ranges:: end ( r ) , value ) ; .

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

first, last - 초기화할 요소들의 범위 를 정의하는 반복자-감시자 쌍
r - 초기화할 요소들의 range 범위
value - 요소들을 생성할 값

반환값

위에서 설명한 대로입니다.

복잡도

초기화되지 않은 메모리 영역의 크기에 선형적으로 비례합니다.

예외

대상 범위의 요소 생성 과정에서 발생하는 모든 예외.

참고 사항

구현체는 출력 범위의 값 타입이 TrivialType 인 경우, 예를 들어 ranges::fill 을 사용함으로써 ranges::uninitialized_fill 의 효율을 향상시킬 수 있습니다.

Feature-test 매크로 표준 기능
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr for 특수화된 메모리 알고리즘 , ( 1,2 )

가능한 구현

struct uninitialized_fill_fn
{
    template<no-throw-forward-iterator I, no-throw-sentinel-for<I> S, class T>
        requires std::constructible_from<std::iter_value_t<I>, const T&>
    constexpr I operator()(I first, S last, const T& value) const
    {
        I rollback{first};
        try
        {
            for (; !(first == last); ++first)
                ranges::construct_at(std::addressof(*first), value);
            return first;
        }
        catch (...)
        {   
            // 롤백: 생성된 요소들 파괴
            for (; rollback != first; ++rollback)
                ranges::destroy_at(std::addressof(*rollback));
            throw;
        }
    }
    template<no-throw-forward-range R, class T>
        requires std::constructible_from<ranges::range_value_t<R>, const T&>
    constexpr ranges::borrowed_iterator_t<R> operator()(R&& r, const T& value) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value);
    }
};
inline constexpr uninitialized_fill_fn uninitialized_fill{};

예제

#include <iostream>
#include <memory>
#include <string>
int main()
{
    constexpr int n{4};
    alignas(alignof(std::string)) char out[n * sizeof(std::string)];
    try
    {
        auto first{reinterpret_cast<std::string*>(out)};
        auto last{first + n};
        std::ranges::uninitialized_fill(first, last, "▄▀▄▀▄▀▄▀");
        int count{1};
        for (auto it{first}; it != last; ++it)
            std::cout << count++ << ' ' << *it << '\n';
        std::ranges::destroy(first, last);
    }
    catch(...)
    {
        std::cout << "Exception!\n";
    }
}

출력:

1 ▄▀▄▀▄▀▄▀
2 ▄▀▄▀▄▀▄▀
3 ▄▀▄▀▄▀▄▀
4 ▄▀▄▀▄▀▄▀

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3870 C++20 이 알고리즘은 const 저장 공간에 객체를 생성할 수 있음 허용되지 않음으로 유지

참고 항목

객체를 시작점과 개수로 정의된 초기화되지 않은 메모리 영역에 복사합니다
(알고리즘 함수 객체)
객체를 범위로 정의된 초기화되지 않은 메모리 영역에 복사합니다
(함수 템플릿)