Namespaces
Variants

std::ranges:: uninitialized_move, std::ranges:: uninitialized_move_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, std:: sentinel_for < I > S1,

no-throw-forward-iterator O, no - throw - sentinel - for < O > S2 >
requires std:: constructible_from < std:: iter_value_t < O > ,
std:: iter_rvalue_reference_t < I >>
uninitialized_move_result < I, O >

uninitialized_move ( I ifirst, S1 ilast, O ofirst, S2 olast ) ;
(1) (C++20부터)
(C++26부터 constexpr)
template < ranges:: input_range IR, no-throw-forward-range OR >

requires std:: constructible_from
< ranges:: range_value_t < OR > ,
ranges:: range_rvalue_reference_t < IR >>
uninitialized_move_result < ranges:: borrowed_iterator_t < IR > ,
ranges:: borrowed_iterator_t < OR >>

uninitialized_move ( IR && in_range, OR && out_range ) ;
(2) (C++20부터)
(C++26부터 constexpr)
헬퍼 타입
template < class I, class O >
using uninitialized_move_result = ranges:: in_out_result < I, O > ;
(3) (C++20부터)

N ranges:: min ( ranges:: distance ( ifirst, ilast ) , ranges:: distance ( ofirst, olast ) ) 로 정의합니다.

1) N 개의 요소를 [ ifirst , ilast ) 에서 초기화되지 않은 메모리 영역 [ ofirst , olast ) 로 복사합니다(지원되는 경우 이동 의미론을 사용).

for ( ; ifirst ! = ilast && ofirst ! = olast ; ++ ofirst, ( void ) ++ ifirst )
:: new ( voidify ( * ofirst ) )
std:: remove_reference_t < std:: iter_reference_t < O >> ( ranges:: iter_move ( ifirst ) ) ;
return { std :: move ( ifirst ) , ofirst } ;

초기화 과정에서 예외가 발생하면 이미 생성된 객체들( [ ofirst , olast ) 범위 내)이 지정되지 않은 순서로 파괴됩니다. 또한 이미 이동된 객체들( [ ifirst , ilast ) 범위 내)은 유효하지만 지정되지 않은 상태로 남게 됩니다.
2) 다음에 해당함: return ranges :: uninitialized_move ( ranges:: begin ( in_range ) , ranges:: end ( in_range ) ,
ranges:: begin ( out_range ) , ranges:: end ( out_range ) ) ;
.

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

목차

매개변수

ifirst, ilast - 이동할 원소들의 입력 범위 를 정의하는 반복자-감시자 쌍
in_range - 이동할 원소들의 입력 range 범위
ofirst, olast - 초기화할 원소들의 출력 범위 를 정의하는 반복자-감시자 쌍
out_range - 초기화할 출력 range 범위

반환값

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

복잡도

선형적으로 N 에 비례합니다.

예외

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

참고 사항

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

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

가능한 구현

struct uninitialized_move_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S1,
             no-throw-forward-iterator O, no-throw-sentinel-for<O> S2>
        requires std::constructible_from<std::iter_value_t<O>,
                                         std::iter_rvalue_reference_t<I>>
    constexpr ranges::uninitialized_move_result<I, O>
        operator()(I ifirst, S1 ilast, O ofirst, S2 olast) const
    {
        using ValueType = std::remove_reference_t<std::iter_reference_t<O>>;
        O current{ofirst};
        try
        {
            for (; !(ifirst == ilast or current == olast); ++ifirst, ++current)
                ::new (static_cast<void*>(std::addressof(*current))))
                    ValueType(ranges::iter_move(ifirst));
            return {std::move(ifirst), std::move(current)};
        }
        catch (...) // 롤백: 생성된 요소들 파괴
        {
            for (; ofirst != current; ++ofirst)
                ranges::destroy_at(std::addressof(*ofirst));
            throw;
        }
    }
    template<ranges::input_range IR, no-throw-forward-range OR>
        requires std::constructible_from<ranges::range_value_t<OR>,
                                         ranges::range_rvalue_reference_t<IR>>
    constexpr ranges::uninitialized_move_result<ranges::borrowed_iterator_t<IR>,
                                                ranges::borrowed_iterator_t<OR>>
        operator()(IR&& in_range, OR&& out_range) const
    {
        return (*this)(ranges::begin(in_range), ranges::end(in_range),
                       ranges::begin(out_range), ranges::end(out_range));
    }
};
inline constexpr uninitialized_move_fn uninitialized_move{};

예제

#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", "World"};
    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(std::begin(in), std::end(in), 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: "Home" "World"
after move, in: "" ""
after move, out: "Home" "World"

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

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

참고 항목

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