std::uninitialized_move
ko.cppreference.net 소개
| 헤더 <memory>
|
||
template< class InputIt, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_move( InputIt first, InputIt last,
NoThrowForwardIt d_first );
|
에 정의됨 (1) | (C++17부터) (C++26부터 constexpr) |
template< class ExecutionPolicy,
class ForwardIt, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_move( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
NoThrowForwardIt d_first );
|
(2) | (C++17부터) |
1) 다음에 의해
d_first 에서 시작하는 대상 범위에 [first, last) 소스 범위의 요소로부터 요소를 생성합니다.
for (; first != last; ++d_first, (void) ++first)
::new (voidify(*d_first))
typename std::iterator_traits<NoThrowForwardIt>::value_type(deref-move(*iter));
return d_first;
초기화 중 예외가 발생하면
[first, last) 의 일부 객체는 유효하지만 지정되지 않은 상태로 남고 이미 생성된 객체는 지정되지 않은 순서로 소멸됩니다.2) (1) 과 동일하지만 에 따라 실행됩니다
policy. 이 오버로드는 다음 표현식의 값이
true 인 경우에만 오버로드 해결에 참여합니다.
|
|
(C++20까지) |
|
|
(C++20부터) |
|
만약 |
(C++20부터) |
매개변수
| first, last | - | 이동할 요소들의 범위 를 정의하는 반복자 쌍 |
| d_first | - | 대상 범위의 시작 지점 |
| policy | - | 사용할 실행 정책 |
| 타입 요구사항 | ||
-
InputIt
는
LegacyInputIterator
요구사항을 충족해야 함
|
||
-
ForwardIt
는
LegacyForwardIterator
요구사항을 충족해야 함
|
||
-
NoThrowForwardIt
는
LegacyForwardIterator
요구사항을 충족해야 함
|
||
-
NoThrowForwardIt
의 유효한 인스턴스를 통한 증가, 할당, 비교, 간접 참조 연산은 예외를 발생시켜서는 안 됨
|
||
반환값
위에서 설명한 바와 같습니다.
예외
2) 실행 과정 중:
- 병렬화에 필요한 임시 메모리 자원을 사용할 수 없는 경우 std::bad_alloc이 발생합니다.
- 알고리즘 인수를 통해 객체에 접근하는 도중 포착되지 않은 예외가 발생하면, 실행 정책에 따라 동작이 결정됩니다 (표준 정책들의 경우 std::terminate가 호출됩니다).
참고
입력 반복자가 rvalue를 역참조하는 경우, std::uninitialized_move의 동작은 std::uninitialized_copy와 동일합니다.
| 기능-테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 대상 특수화된 <memory> 알고리즘, (1)
|
가능한 구현
template<class InputIt, class NoThrowForwardIt> constexpr NoThrowForwardIt uninitialized_move(InputIt first, InputIt last, NoThrowForwardIt d_first) { using ValueType = typename std::iterator_traits<NoThrowForwardIt>::value_type; auto current = d_first; try { for (; first != last; ++first, (void) ++current) { auto addr = static_cast<void*>(std::addressof(*current)); if constexpr (std::is_lvalue_reference_v<decltype(*first)>) ::new (addr) ValueType(std::move(*first)); else ::new (addr) ValueType(*first); } return current; } catch (...) { std::destroy(d_first, current); throw; } } |
예제
이 코드 실행하기
#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[]{"Home", "Work!"};
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::uninitialized_move(std::begin(in), std::end(in), first);
print("after move, in: ", std::begin(in), std::end(in));
print("after move, out: ", first, last);
std::destroy(first, last);
}
catch (...)
{
std::cout << "Exception!\n";
}
std::free(out);
}
}
가능한 출력:
initially, in: "Home" "Work!"
after move, in: "" ""
after move, out: "Home" "Work!"
결함 보고서
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급하여 적용되었습니다.
| DR | 적용 대상 | 발표된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 3918 | C++17 | 입력 반복자가 prvalue를 역참조하는 경우에도 항상 이동 생성이 수행됨 |
이 경우 이동 생성 없음 |
참고 항목
|
객체 범위를 초기화되지 않은 메모리 영역으로 복사합니다
(function template) |
|
|
(C++17)
|
지정된 개수의 객체를 초기화되지 않은 메모리 영역으로 이동합니다
(function template) |
|
(C++20)
|
객체 범위를 초기화되지 않은 메모리 영역으로 이동합니다
(algorithm function object) |