memory_uninitialized_construct_using_allocator
memory_uninitialized_construct_using_allocator (std::uninitialized_construct_using_allocator)
<memory> 헤더에 정의되어 있어요. p가 가리키는 초기화되지 않은 메모리 위치에 uses-allocator construction을 통해 주어진 타입 T의 객체를 생성해요. C++20부터 도입됐어요.
출처: cppreference
본문
template< class T, class Alloc, class... Args >
constexpr T* uninitialized_construct_using_allocator( T* p,
const Alloc& alloc,
Args&&... args );
(C++20부터)
p가 가리키는 초기화되지 않은 메모리 위치에 uses-allocator construction을 통해 주어진 타입 T의 객체를 생성해요. 다음과 동등해요.
return std::apply(
[&]<class... Xs>(Xs&&...xs)
{
return std::construct_at(p, std::forward<Xs>(xs)...);
},
std::uses_allocator_construction_args<T>(alloc, std::forward<Args>(args)...));
매개변수
p: 객체가 놓일 메모리 위치alloc: 사용할 할당자args:T의 생성자에 전달할 인자들
반환값
새로 생성된 T 타입 객체에 대한 포인터.
예외
T의 생성자가 던지는 예외를 던질 수 있고, 보통 std::bad_alloc을 포함해요.
이 함수는 std::construct_at의 할당자 인식 버전으로, 할당자 인식 타입을 초기화되지 않은 메모리에 구성할 때 할당자를 올바르게 전달하면서 객체를 만들어요.
alignas(Widget) std::byte buf[sizeof(Widget)];
Widget* w = std::uninitialized_construct_using_allocator<Widget>(
reinterpret_cast<Widget*>(buf), alloc, args...);