std::ranges::uninitialized_move_n, std::ranges::uninitialized_move_n_result
cppreference.net에서
| 헤더 <memory>
|
||
| 에 정의됨 |
||
template< std::input_iterator I,
/*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S >
requires std::constructible_from<std::iter_value_t<O>,
std::iter_rvalue_reference_t<I>>
uninitialized_move_n_result<I, O>
uninitialized_move_n( I ifirst, std::iter_difference_t<I> count,
O ofirst, S olast );
|
호출 서명 | (1) (C++20부터) |
template< /*execution-policy*/ Ep, std::random_access_iterator I,
/*nothrow-random-access-iterator*/ O,
/*nothrow-sized-sentinel-for*/<O> S >
requires std::constructible_from<std::iter_value_t<O>,
std::iter_rvalue_reference_t<I>>
uninitialized_move_n_result<I, O>
uninitialized_move_n( Ep&& policy, I ifirst,
std::iter_difference_t<I> count,
O ofirst, S olast );
|
(C++26부터 constexpr) | (2) |
| (C++26부터) |
||
template< class I, class O >
using uninitialized_move_n_result = ranges::in_out_result<I, O>;
|
도우미 타입 | (3) |
(C++20부터)/*execution-policy*/의 정의는 이 페이지를 참조하십시오; 다른 설명 전용 개념의 정의는 이 페이지를 참조하십시오.
1) 다음과 같이 소스 범위
[ofirst, olast) 의 요소들로부터 대상 범위 ifirst + [0, count) 에 요소들을 생성합니다:
auto ret = ranges::uninitialized_move(std::counted_iterator(std::move(ifirst), count),
std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};
초기화 중에 예외가 발생하면
[ofirst, olast) 에 이미 생성된 객체들은 지정되지 않은 순서로 소멸됩니다. 또한, 이미 이동된, ifirst 에서 시작하는 입력 범위의 객체들은 유효하지만 지정되지 않은 상태로 남습니다. 만약
[ofirst, olast) 이 ifirst + [0, count) 와 겹치면, 동작은 정의되지 않습니다.2) (1) 과 동일하지만,
policy 에 따라 실행됩니다.이 페이지에 설명된 함수형 개체들은 알고리즘 함수 객체입니다 (비공식적으로 niebloids라고도 함). 즉:
- 호출 시 명시적 템플릿 인자 목록을 지정할 수 없습니다.
- 그들 중 어떤 것도 인자 종속 조회에 보이지 않습니다.
- 이들 중 어떤 것이 일반적인 비한정 조회에 의해 함수 호출 연산자의 왼쪽에 있는 이름으로 발견되면, 인자 종속 조회가 억제됩니다.
매개변수
| ifirst | - | 이동할 입력 범위 요소의 시작 |
| ofirst, olast | - | 초기화할 출력 범위의 요소를 정의하는 반복자-종단자 쌍 |
| count | - | 이동할 요소의 개수 |
| policy | - | 사용할 실행 정책 |
반환값
위에서 설명한 바와 같습니다.
예외
대상 범위의 요소를 생성하는 중에 발생한 모든 예외.
2) 실행 과정 중:
- 병렬화에 필요한 임시 메모리 자원을 사용할 수 없는 경우 std::bad_alloc이 발생합니다.
- 알고리즘 인수를 통해 객체에 접근하는 동안 처리되지 않은 예외가 발생하면, 실행 정책에 따라 동작이 결정됩니다(표준 정책의 경우 std::terminate이 호출됩니다).
참고 사항
출력 범위의 값 유형이 ranges::uninitialized_move_nTrivialType인 경우 구현에서는 (예를 들어 ranges::copy_n을 사용하여) 의 효율성을 향상시킬 수 있습니다.
| 기능-테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | 병렬 범위 알고리즘 |
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 전문화된 알고리즘<memory>을 위한 , (1)
|
가능한 구현
struct uninitialized_move_n_fn
{
template<std::input_iterator I,
/*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S>
requires std::constructible_from<std::iter_value_t<O>,
std::iter_rvalue_reference_t<I>>
constexpr ranges::uninitialized_move_n_result<I, O>
operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const
{
auto iter = std::counted_iterator(std::move(ifirst), count);
auto ret = ranges::uninitialized_move(iter, std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};
}
};
inline constexpr uninitialized_move_n_fn uninitialized_move_n{};
|
예제
이 코드 실행하기
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
void print(auto rem, auto first, auto last)
{
for (std::cout << rem; first != last; ++first)
std::cout << std::quoted(*first) << ' ';
std::cout << '\n';
}
int main()
{
std::string in[]{ "No", "Diagnostic", "Required", };
print("initially, in: ", std::begin(in), std::end(in));
if (constexpr auto sz = std::size(in);
void* out = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz)
)
{
try
{
auto first{static_cast<std::string*>(out)};
auto last{first + sz};
std::ranges::uninitialized_move_n(std::begin(in), sz, first, last);
print("after move, in: ", std::begin(in), std::end(in));
print("after move, out: ", first, last);
std::ranges::destroy(first, last);
}
catch (...)
{
std::cout << "Exception!\n";
}
std::free(out);
}
}
가능한 출력:
initially, in: "No" "Diagnostic" "Required"
after move, in: "" "" ""
after move, out: "No" "Diagnostic" "Required"
참고 항목
(C++20) |
객체의 범위를 초기화되지 않은 메모리 영역으로 이동합니다 (알고리즘 함수 객체) |
(C++17) |
여러 객체를 초기화되지 않은 메모리 영역으로 이동합니다 (함수 템플릿) |