std::ranges:: uninitialized_default_construct
|
헤더 파일에 정의됨
<memory>
|
||
|
함수 시그니처
|
||
|
template
<
no-throw-forward-iterator
I, no
-
throw
-
sentinel
-
for
<
I
>
S
>
requires
std::
default_initializable
<
std::
iter_value_t
<
I
>>
|
(1) |
(C++20부터)
(C++26부터 constexpr) |
|
template
<
no-throw-forward-range
R
>
requires
std::
default_initializable
<
ranges::
range_value_t
<
R
>>
|
(2) |
(C++20부터)
(C++26부터 constexpr) |
[
first
,
last
)
에서
std::
iter_value_t
<
I
>
타입의 객체들을
기본 초기화
를 통해 생성합니다. 다음과 같이 구현된 것처럼:
for
(
;
first
!
=
last
;
++
first
)
::
new
(
voidify
(
*
first
)
)
std::
remove_reference_t
<
std::
iter_reference_t
<
I
>>
;
return
first
;
이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진) 즉:
- 명시적 템플릿 인수 목록은 이들 중 어느 것을 호출할 때도 지정할 수 없습니다.
- 이들 중 어느 것도 인수 의존 이름 검색 에 보이지 않습니다.
- 이들 중 어느 것이 함수 호출 연산자 왼쪽의 이름으로 일반 비한정 이름 검색 에 의해 발견될 때, 인수 의존 이름 검색 이 억제됩니다.
목차 |
매개변수
| first, last | - | 초기화할 요소들의 범위 를 정의하는 반복자-감시자 쌍 |
| r | - |
초기화할 요소들의
range
범위
|
반환값
위에서 설명한 바와 같습니다.
복잡도
first 와 last 사이의 거리에 선형적으로 비례합니다.
예외
대상 범위의 요소 생성 과정에서 발생하는 모든 예외.
참고 사항
구현은 std:: iter_value_t < I > 객체를 기본 초기화하는 동안 비트리비얼 기본 생성자가 호출되지 않는 경우, 객체의 생성을 건너뛸 수 있습니다(관찰 가능한 효과를 변경하지 않고). 이는 std::is_trivially_default_constructible 로 감지할 수 있습니다.
| Feature-test 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr for 특수화된 메모리 알고리즘 , ( 1,2 ) |
가능한 구현
struct uninitialized_default_construct_fn { template<no-throw-forward-iterator I, no-throw-sentinel-for<I> S> requires std::default_initializable<std::iter_value_t<I>> constexpr I operator()(I first, S last) const { using ValueType = std::remove_reference_t<std::iter_reference_t<I>>; if constexpr (std::is_trivially_default_constructible_v<ValueType>) return ranges::next(first, last); // 초기화 건너뜀 I rollback{first}; try { for (; !(first == last); ++first) ::new (static_cast<void*>(std::addressof(*first))) ValueType; return first; } catch (...) // 롤백: 생성된 요소들 파괴 { for (; rollback != first; ++rollback) ranges::destroy_at(std::addressof(*rollback)); throw; } } template<no-throw-forward-range R> requires std::default_initializable<ranges::range_value_t<R>> constexpr ranges::borrowed_iterator_t<R> operator()(R&& r) const { return (*this)(ranges::begin(r), ranges::end(r)); } }; inline constexpr uninitialized_default_construct_fn uninitialized_default_construct{}; |
예제
#include <cstring> #include <iostream> #include <memory> #include <string> int main() { struct S { std::string m{"▄▀▄▀▄▀▄▀"}; }; constexpr int n{4}; alignas(alignof(S)) char out[n * sizeof(S)]; try { auto first{reinterpret_cast<S*>(out)}; auto last{first + n}; std::ranges::uninitialized_default_construct(first, last); auto count{1}; for (auto it{first}; it != last; ++it) std::cout << count++ << ' ' << it->m << '\n'; std::ranges::destroy(first, last); } catch (...) { std::cout << "Exception!\n"; } // "trivial types"의 경우 uninitialized_default_construct는 // 일반적으로 주어진 초기화되지 않은 메모리 영역을 0으로 채우지 않습니다. constexpr char sample[]{'A', 'B', 'C', 'D', '\n'}; char v[]{'A', 'B', 'C', 'D', '\n'}; std::ranges::uninitialized_default_construct(std::begin(v), std::end(v)); if (std::memcmp(v, sample, sizeof(v)) == 0) { std::cout << " "; // 정의되지 않은 동작일 수 있음, CWG 1997 해결 대기 중: // for (const char c : v) { std::cout << c << ' '; } for (const char c : sample) std::cout << c << ' '; } else std::cout << "Unspecified\n"; }
가능한 출력:
1 ▄▀▄▀▄▀▄▀ 2 ▄▀▄▀▄▀▄▀ 3 ▄▀▄▀▄▀▄▀ 4 ▄▀▄▀▄▀▄▀ A B C D
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 3870 | C++20 | 이 알고리즘이 const 저장 공간에 객체를 생성할 수 있음 | 허용되지 않음으로 유지 |
참고 항목
|
시작점과 개수로 정의된 초기화되지 않은 메모리 영역에서
기본 초기화
를 통해 객체를 생성함
(알고리즘 함수 객체) |
|
|
범위로 정의된 초기화되지 않은 메모리 영역에서
값 초기화
를 통해 객체를 생성함
(알고리즘 함수 객체) |
|
|
시작점과 개수로 정의된 초기화되지 않은 메모리 영역에서
값 초기화
를 통해 객체를 생성함
(알고리즘 함수 객체) |
|
|
(C++17)
|
범위로 정의된 초기화되지 않은 메모리 영역에서
기본 초기화
를 통해 객체를 생성함
(함수 템플릿) |