Namespaces
Variants

std::ranges:: uninitialized_default_construct_n

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 < no-throw-forward-iterator I >

requires std:: default_initializable < std:: iter_value_t < I >>
I uninitialized_default_construct_n ( I first,

std:: iter_difference_t < I > count ) ;
(C++20부터)
(C++26부터 constexpr)

초기화되지 않은 메모리 영역 first + [ 0 , count ) std:: iter_value_t < I > 타입의 객체들을 기본 초기화 를 통해 생성합니다. 마치 다음과 같이 수행하는 것과 같습니다: return ranges:: uninitialized_default_construct ( std:: counted_iterator ( first, count ) ,
std:: default_sentinel ) . base ( ) ;

초기화 과정에서 예외가 발생하면, 이미 생성된 객체들은 지정되지 않은 순서로 파괴됩니다.

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

목차

매개변수

first - 초기화할 요소 범위의 시작
count - 생성할 요소의 개수

반환값

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

복잡도

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

예외

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

참고 사항

구현은 std:: iter_value_t < I > 객체를 기본 초기화하는 동안 비트리비얼 기본 생성자가 호출되지 않는 경우, 객체의 생성을 건너뛸 수 있습니다(관찰 가능한 효과를 변경하지 않고). 이는 std::is_trivially_default_constructible 로 감지할 수 있습니다.

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr for 특수화된 메모리 알고리즘

가능한 구현

struct uninitialized_default_construct_n_fn
{
    template<no-throw-forward-iterator I>
        requires std::default_initializable<std::iter_value_t<I>>
    constexpr I operator()(I first, std::iter_difference_t<I> count) const
    {
        auto iter = std::counted_iterator(first, count);
        return ranges::uninitialized_default_construct(iter, std::default_sentinel).base();
    }
};
inline constexpr uninitialized_default_construct_n_fn uninitialized_default_construct_n{};

예제

#include <cstring>
#include <iostream>
#include <memory>
#include <string>
int main()
{
    struct S { std::string m{"█▓▒░ █▓▒░ "}; };
    constexpr int n{4};
    alignas(alignof(S)) char out[n * sizeof(S)];
    try
    {
        auto first{reinterpret_cast<S*>(out)};
        auto last = std::ranges::uninitialized_default_construct_n(first, n);
        auto count{1};
        for (auto it{first}; it != last; ++it)
            std::cout << count++ << ' ' << it->m << '\n';
        std::ranges::destroy(first, last);
    }
    catch (...)
    {
        std::cout << "Exception!\n";
    }
    // 스칼라 타입의 경우, uninitialized_default_construct_n은
    // 일반적으로 주어진 초기화되지 않은 메모리 영역을 0으로 채우지 않습니다.
    constexpr int sample[]{1, 2, 3, 4, 5, 6};
    int v[]{1, 2, 3, 4, 5, 6};
    std::ranges::uninitialized_default_construct_n(std::begin(v), std::size(v));
    if (std::memcmp(v, sample, sizeof(v)) == 0)
    {
        // CWG 1997에 따라 미정의 동작일 수 있음:
        // for (const int i : v) { std::cout << i << ' '; }
        for (const int i : sample)
            std::cout << i << ' ';
    }
    else
        std::cout << "Unspecified!";
    std::cout << '\n';
}

가능한 출력:

1 █▓▒░ █▓▒░
2 █▓▒░ █▓▒░
3 █▓▒░ █▓▒░
4 █▓▒░ █▓▒░
1 2 3 4 5 6

결함 보고서

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

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

참고 항목

범위로 정의된 초기화되지 않은 메모리 영역에서 기본 초기화 를 통해 객체를 생성함
(알고리즘 함수 객체)
범위로 정의된 초기화되지 않은 메모리 영역에서 값 초기화 를 통해 객체를 생성함
(알고리즘 함수 객체)
시작점과 개수로 정의된 초기화되지 않은 메모리 영역에서 값 초기화 를 통해 객체를 생성함
(알고리즘 함수 객체)
시작점과 개수로 정의된 초기화되지 않은 메모리 영역에서 기본 초기화 를 통해 객체를 생성함
(함수 템플릿)