Namespaces
Variants

std::ranges:: uninitialized_copy, std::ranges:: uninitialized_copy_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_reference_t < I >>
uninitialized_copy_result < I, O >

uninitialized_copy ( 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_reference_t < IR >>
uninitialized_copy_result < ranges:: borrowed_iterator_t < IR > ,
ranges:: borrowed_iterator_t < OR >>

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

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

1) 범위 [ ifirst , ilast ) 로부터 N 개의 요소들을 초기화되지 않은 메모리 영역 [ ofirst , olast ) 로 다음과 같이 생성합니다

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

예외가 초기화 과정 중에 발생하면, 이미 생성된 객체들은 지정되지 않은 순서로 파괴됩니다.
만약 [ ofirst , olast ) [ ifirst , ilast ) 와 겹치는 경우, 동작은 정의되지 않습니다.
2) 다음에 해당함: return ranges :: uninitialized_copy ( 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::uninitialized_copy 의 효율성을 향상시킬 수 있습니다.

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

가능한 구현

struct uninitialized_copy_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_reference_t<I>>
    constexpr ranges::uninitialized_copy_result<I, O>
        operator()(I ifirst, S1 ilast, O ofirst, S2 olast) const
    {
        O current{ofirst};
        try
        {
            for (; !(ifirst == ilast or current == olast); ++ifirst, ++current)
                ranges::construct_at(std::addressof(*current), *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>,
    constexpr ranges::range_reference_t<IR>>
        ranges::uninitialized_copy_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_copy_fn uninitialized_copy{};

예제

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
int main()
{
    const char* v[]{"This", "is", "an", "example"};
    if (const auto sz{std::size(v)};
        void* pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz))
    {
        try
        {
            auto first{static_cast<std::string*>(pbuf)};
            auto last{first + sz};
            std::ranges::uninitialized_copy(std::begin(v), std::end(v), first, last);
            std::cout << "{";
            for (auto it{first}; it != last; ++it)
                std::cout << (it == first ? "" : ", ") << std::quoted(*it);
            std::cout << "};\n";
            std::ranges::destroy(first, last);
        }
        catch (...)
        {
            std::cout << "uninitialized_copy exception\n";
        }
        std::free(pbuf);
    }
}

출력:

{"This", "is", "an", "example"};

결함 보고서

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

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

참고 항목

지정된 개수의 객체를 초기화되지 않은 메모리 영역에 복사합니다
(알고리즘 함수 객체)
객체 범위를 초기화되지 않은 메모리 영역에 복사합니다
(함수 템플릿)