ranges_uninitialized_fill

ranges_uninitialized_fill (ranges::uninitialized_fill)

<memory> 헤더에 정의되어 있어요. 대상 범위의 요소들을 주어진 값 value로 초기화해 구성해요. std::uninitialized_fill의 ranges 버전(니블로이드, niebloid)이에요. C++20부터 도입됐어요.

출처: cppreference

본문

호출 시그니처:

template< /*nothrow-forward-iterator*/ I, /*nothrow-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) since C++20, constexpr since C++26

template< /*nothrow-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) since C++20, constexpr since C++26

template< /*execution-policy*/ Ep, /*nothrow-random-access-iterator*/ I,
          /*nothrow-sized-sentinel-for*/<I> S,
          class T = std::iter_value_t<I> >
    requires constructible_from<std::iter_value_t<I>, const T&>
I uninitialized_fill( Ep&& policy, I first, S last, const T& value );   // (3) since C++26
  • (1) 대상 범위 [first, last)의 요소들을 주어진 값 value로 구성해요. 초기화 중 예외가 발생하면 이미 구성된 객체들이 미지정 순서로 파괴돼요.
  • (2) (1)과 같지만 r을 대상 범위로 사용해요.
  • (3)(4) (1,2)와 같지만 policy에 따라 실행돼요. (C++26)

매개변수

  • first, last: 초기화할 요소 범위를 정의하는 반복자·센티널 쌍
  • r: 초기화할 요소 범위
  • value: 요소를 구성할 값
  • policy: 사용할 실행 정책

반환값

마지막으로 구성한 요소 다음의 반복자.

예외

대상 범위 요소 구성 중 던져진 어떤 예외든. (3)(4)에서 병렬화 임시 메모리가 없으면 std::bad_alloc.

주의 (Notes)

출력 범위의 값 타입이 TrivialType이면 구현이 ranges::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 ▄▀▄▀▄▀▄▀

더 알아보기 (Learn more)

cppreference