Namespaces
Variants

std::uninitialized_copy

cppreference.net에서
 
 
알고리즘 라이브러리
제한된 알고리즘 및 범위 기반 알고리즘 (C++20부터)
제한된 알고리즘, 예: ranges::copy, ranges::sort, ...
비수정 시퀀스 연산    
일괄 연산
(C++17부터)
검색 연산
(C++11부터)(C++11부터)(C++11부터)

수정 시퀀스 연산
복사 연산
(C++11부터)
(C++11부터)
(C++11부터)
(C++11부터)
스왑 연산
변환 연산
생성 연산
제거 연산
순서 변경 연산
(C++17까지)(C++11부터)
(C++20부터)(C++20부터)
샘플링 연산
(C++17부터)

정렬 및 관련 연산
분할 연산
(C++11부터)    
(C++11부터)

정렬 연산
(C++11부터)
(C++11부터)

이진 검색 연산
(분할된 범위에 대해)
집합 연산 (정렬된 범위에 대해)
병합 연산 (정렬된 범위에 대해)
힙 연산
(C++11부터)
(C++11부터)
최소/최대 연산
(C++11부터)
(C++17부터)
사전식 비교 연산
순열 연산
(C++11부터)




실행 정책 (C++17부터)
(C++17부터)
(C++17부터)(C++17부터)(C++17부터)(C++20부터)
(설명 전용*)(C++26부터)

수치 연산
(C++11부터)
(C++17부터)
(C++17부터)
(C++17부터)    
(C++17부터)

특수화된 <memory> 알고리즘

특수화된 <random> 알고리즘
C 라이브러리









 
특수화된 <메모리> 알고리즘
헬퍼 템플릿 (설명 전용*)
(C++17부터)
객체 (일괄) 생성
객체 (일괄) 소멸
(C++17부터)
(C++17부터)
(C++17부터)
(C++20부터)
(C++20부터)
(C++20부터)
 
헤더에 정의됨 <memory>
template< class InputIt, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_copy( InputIt first, InputIt last,
                                     NoThrowForwardIt d_first );
(1) (C++26부터 constexpr)
template< class ExecutionPolicy, class ForwardIt,
          class NoThrowForwardIt >
NoThrowForwardIt uninitialized_copy( 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(*first);
return d_first;

초기화 중 예외가 발생하면 이미 생성된 객체들은 지정되지 않은 순서로 소멸됩니다.
2) (1)과 동일하지만 policy에 따라 실행됩니다.
이 오버로드는 다음 표현식의 값이 true인 경우에만 오버로드 해결에 참여합니다:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20까지)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20부터)


만약 d_first + [0, std::distance(first, last))이 [first, last)과 중첩되면, 동작은 정의되지 않습니다.

(C++20부터)

매개변수

first, last - 복사할 요소들의 범위 를 정의하는 반복자 쌍
d_first - 대상 범위의 시작 지점
policy - 사용할 실행 정책
타입 요구사항
-
InputIt 는 LegacyInputIterator 요구사항을 충족해야 함
-
ForwardIt 는 LegacyForwardIterator 요구사항을 충족해야 함
-
NoThrowForwardIt 는 LegacyForwardIterator 요구사항을 충족해야 함
-
NoThrowForwardIt 의 유효한 인스턴스를 통한 증가, 할당, 비교, 간접 참조 연산은 예외를 발생시키지 않아야 함 NoThrowForwardIt 값에 & * 를 적용하면 해당 값 타입의 포인터를 반환해야 함 (C++11 이전)

반환값

위에서 설명한 대로.

예외

2) 실행 과정 중:
  • 병렬화에 필요한 임시 메모리 자원을 사용할 수 없는 경우, std::bad_alloc이 발생합니다.
  • 알고리즘 인수를 통해 객체에 접근하는 동안 처리되지 않은 예외가 발생하면, 동작은 실행 정책에 따라 결정됩니다(표준 정책의 경우 std::terminate가 호출됩니다).

참고

기능 테스트 매크로 값 표준 기능
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 용 특수화된 <memory> 알고리즘, (1)

가능한 구현

template<class InputIt, class NoThrowForwardIt>
constexpr NoThrowForwardIt uninitialized_copy(InputIt first, InputIt last,
                                              NoThrowForwardIt d_first)
{
    using T = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = d_first;
    try
    {
        for (; first != last; ++first, (void) ++current)
            ::new (static_cast<void*>(std::addressof(*current))) T(*first);
        return current;
    }
    catch (...)
    {
        for (; d_first != current; ++d_first)
            d_first->~T();
        throw;
    }
}

예제

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

출력:

This_is_an_example_

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 866 C++98 주어진 T 값 형식으로 NoThrowForwardIt, 만약
T::operator new 존재하면, 프로그램이 잘못된 형식일 수 있음
전역 대체-
new 대신
LWG 2133 C++98 효과 설명은 for 루프에 반복
표현식 ++d_first, ++first, 이는
의 인수 종속 조회에서operator,
값을 무시함
한 피연산자의
해당 ADL을 비활성화하기 위해
LWG 2433 C++11 이 알고리즘은 오버로드된 operator& 사용 std::addressof에 의해 하이재킹될 수 있음

참고 항목

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