std::ranges::uninitialized_fill_n
cppreference.net에서
| 헤더에 정의됨 <memory>
|
||
| 호출 서명 |
||
template< /*nothrow-forward-iterator*/ I, class T >
requires std::constructible_from<std::iter_value_t<I>, const T&>
I uninitialized_fill_n( I first, std::iter_difference_t<I> count,
const T& value );
|
(1) | (C++20부터) (C++26부터 constexpr) |
template< /*execution-policy*/ Ep, /*nothrow-random-access-iterator*/ I,
class T = std::iter_value_t<I> >
requires std::constructible_from<std::iter_value_t<I>, const T&>
I uninitialized_fill_n( Ep&& exec, I first, std::iter_difference_t<I> count,
const T& value );
|
(2) | (C++26부터) |
/*execution-policy*/의 정의는 이 페이지를 참조하십시오; 다른 설명 전용 개념의 정의는 이 페이지를 참조하십시오.
1) 대상 범위
first + [0, count)의 요소들을 지정된 값 value으로 다음과 같이 생성합니다.
return ranges::uninitialized_fill(std::counted_iterator(first, count),
std::default_sentinel, value).base();
초기화 중에 예외가 발생하면 이미 생성된 객체들은 지정되지 않은 순서로 소멸됩니다.
2)(1)과 동일하지만,
policy에 따라 실행됩니다.이 페이지에 설명된 함수형 엔티티는 알고리즘 함수 객체(비공식적으로 니블로이드라고 함)입니다. 즉:
- 명시적 템플릿 인자 목록은 이들을 호출할 때 지정할 수 없습니다.
- 이들 중 어느 것도 인자 종속 조회에 보이지 않습니다.
- 이들 중 하나가 일반 비한정 조회에 의해 함수 호출 연산자 왼쪽의 이름으로 발견되면, 인자 종속 조회가 억제됩니다.
매개변수
| first | - | 초기화할 요소 범위의 시작 |
| count | - | 생성할 요소의 개수 |
| value | - | 요소를 생성할 값 |
| policy | - | 사용할 실행 정책 |
반환값
위에서 설명한 바와 같습니다.
예외
대상 범위의 요소를 생성하는 중에 발생한 모든 예외.
2) 실행 처리 중:
- 병렬화에 필요한 임시 메모리 자원을 사용할 수 없는 경우, std::bad_alloc 가 발생합니다.
- 알고리즘 인수를 통해 객체에 접근하는 동안 처리되지 않은 예외가 발생하면, 동작은 실행 정책에 따라 결정됩니다 (표준 정책, std::terminate가 호출됩니다).
참고
구현은 ranges::uninitialized_fill_n의 효율성을 향상시킬 수 있습니다(예: ranges::fill_n 사용), 만약 출력 범위의 값 타입이 TrivialType인 경우.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | 병렬 범위 알고리즘 |
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 를 위한 전문화된 <memory>알고리즘, (1)
|
가능한 구현
struct uninitialized_fill_n_fn
{
template</*nothrow-forward-range*/ I, class T>
requires std::constructible_from<std::iter_value_t<I>, const T&>
constexpr I operator()(I first, std::iter_difference_t<I> count,
const T& value) const
{
I rollback{first};
try
{
for (; count-- > 0; ++first)
ranges::construct_at(std::addressof(*first), value);
return first;
}
catch (...) // rollback: destroy constructed elements
{
for (; rollback != first; ++rollback)
ranges::destroy_at(std::addressof(*rollback));
throw;
}
}
};
inline constexpr uninitialized_fill_n_fn uninitialized_fill_n{};
|
예제
이 코드 실행
#include <iostream> #include <memory> #include <string> int main() { constexpr int n{3}; alignas(alignof(std::string)) char out[n * sizeof(std::string)]; try { auto first{reinterpret_cast<std::string*>(out)}; auto last = std::ranges::uninitialized_fill_n(first, n, "cppreference"); for (auto it{first}; it != last; ++it) std::cout << *it << '\n'; std::ranges::destroy(first, last); } catch (...) { std::cout << "Exception!\n"; } }
출력:
cppreference cppreference cppreference
같이 보기
(C++20) |
객체를 범위로 지정된 초기화되지 않은 메모리 영역에 복사합니다 (알고리즘 함수 객체) |
| 객체를 시작점과 개수로 지정된 초기화되지 않은 메모리 영역에 복사합니다 (함수 템플릿) |