std:: uninitialized_fill
|
헤더 파일에 정의됨
<memory>
|
||
|
template
<
class
NoThrowForwardIt,
class
T
>
void
uninitialized_fill
(
NoThrowForwardIt first,
|
(1) | (C++26부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
NoThrowForwardIt,
class
T
>
void
uninitialized_fill
(
ExecutionPolicy
&&
policy,
|
(2) | (C++17부터) |
[
first
,
last
)
에 다음과 같이 복사합니다
for
(
;
first
!
=
last
;
++
first
)
::
new
(
voidify
(
*
first
)
)
typename
std::
iterator_traits
<
NoThrowForwardIt
>
::
value_type
(
value
)
;
|
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 | - | 사용할 실행 정책 |
| 타입 요구사항 | ||
-
NoThrowForwardIt
는
LegacyForwardIterator
요구사항을 충족해야 함
|
||
-
NoThrowForwardIt
의 유효한 인스턴스를 통한 증가, 할당, 비교, 간접 참조 연산은 예외를 발생시켜서는 안 됨
NoThrowForwardIt
값에
&
*
를 적용하면 해당 값 타입의 포인터를 반환해야 함
(C++11 이전)
|
||
복잡도
first 와 last 사이의 거리에 선형적으로 비례합니다.
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr for 특수화된 메모리 알고리즘 , ( 1 ) |
가능한 구현
template<class NoThrowForwardIt, class T> constexpr void uninitialized_fill(NoThrowForwardIt first, NoThrowForwardIt last, const T& value) { using V = typename std::iterator_traits<NoThrowForwardIt>::value_type; NoThrowForwardIt 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, "Example"); for (std::string* i = p; i != p + sz; ++i) { std::cout << *i << '\n'; i->~basic_string<char>(); } alloc.deallocate(p, sz); }
출력:
Example Example Example Example
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 수정된 동작 |
|---|---|---|---|
| LWG 866 | C++98 |
NoThrowForwardIt
의 값 타입이
T
일 때,
T :: operator new 가 존재하면 프로그램이 올바르지 않을 수 있음 |
전역 배치 new 를 대신 사용 |
| LWG 2433 | C++11 | 이 알고리즘이 오버로드된 operator & 에 의해 가로채어질 수 있음 | std::addressof 사용 |
| LWG 3870 | C++20 | 이 알고리즘이 const 저장소에 객체를 생성할 수 있음 | 허용되지 않음으로 유지 |
참고 항목
|
시작 위치와 개수로 정의된 초기화되지 않은 메모리 영역에 객체를 복사합니다
(function template) |
|
|
(C++20)
|
범위로 정의된 초기화되지 않은 메모리 영역에 객체를 복사합니다
(algorithm function object) |