Namespaces
Variants

std::ranges:: uninitialized_move_n, std::ranges:: uninitialized_move_n_result

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
헤더 파일에 정의됨 <memory>
호출 시그니처
template < std:: input_iterator I,

no-throw-forward-iterator O, no - throw - 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부터)
(C++26부터 constexpr)
헬퍼 타입
template < class I, class O >
using uninitialized_move_n_result = ranges:: in_out_result < I, O > ;
(2) (C++20부터)

N ranges:: min ( count, ranges:: distance ( ofirst, olast ) ) 라고 하자.

N 개의 요소를 ifirst 로 시작하는 범위에서 초기화되지 않은 메모리 영역 [ ofirst , olast ) 로 복사합니다(지원되는 경우 이동 의미론을 사용). 마치 다음과 같이 동작합니다: 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 ) 와 겹치는 경우, 동작은 정의되지 않습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

ifirst - 이동할 입력 요소 범위의 시작
ofirst, olast - 초기화할 요소의 출력 범위 를 정의하는 반복자-감시자 쌍
n - 이동할 요소의 개수

반환값

위에서 설명한 바와 같습니다.

복잡도

𝓞(N) .

예외

대상 범위의 요소 생성 과정에서 발생하는 모든 예외.

참고 사항

구현체는 출력 범위의 값 타입이 TrivialType 인 경우, 예를 들어 ranges::copy_n 을 사용함으로써 ranges::uninitialized_move_n 의 효율을 향상시킬 수 있습니다.

Feature-test 매크로 표준 기능
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr for 특수화된 메모리 알고리즘 , ( 1 )

가능한 구현

struct uninitialized_move_n_fn
{
    template<std::input_iterator I,
             no-throw-forward-iterator O, no-throw-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++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3870 C++20 이 알고리즘은 const 스토리지에 객체를 생성할 수 있음 허용되지 않음으로 유지

참고 항목

객체 범위를 초기화되지 않은 메모리 영역으로 이동
(알고리즘 함수 객체)
지정된 개수의 객체를 초기화되지 않은 메모리 영역으로 이동
(함수 템플릿)