make_obj_using_allocator — std::make_obj_using_allocator

make_obj_using_allocator — std::make_obj_using_allocator

std::make_obj_using_allocator는 uses-allocator 생성 방식으로 타입 T 객체를 만드는 함수예요. C++20에서 도입됐어요. <memory> 헤더에 제공돼요.

할당자 인식 타입을 만들 때, 그 타입이 할당자를 인자로 받는 생성자를 가졌다면 적절히 전달하고, 아니면 일반 생성자를 사용해 객체를 만들어 반환해요. 제네릭 코드에서 할당자 인식 객체를 안전하게 생성할 때 유용해요.

출처: cppreference

본문

std::make_obj_using_allocator는 uses-allocator 생성을 통해 T 객체를 만들어요.

template<class T, class Alloc, class... Args>
constexpr T make_obj_using_allocator(const Alloc& alloc, Args&&... args);

alloc을 사용해 T 타입 객체를 생성하고 반환해요. T가 할당자 인식 타입이라면 uses_allocator_construction_args에 따라 할당자를 적절한 위치의 생성자 인자로 전달하고, 아니면 args...만으로 일반 생성자를 호출해요.

auto obj = std::make_obj_using_allocator<std::vector<int>>(alloc, 10);

이 함수 덕분에 제네릭 코드가 "이 타입이 할당자를 쓰는지 아닌지"를 모른 채로도, 할당자 인식이라면 할당자를 올바르게 전달해 객체를 만들 수 있어요.

같이 보기

  • std::uses_allocator_construction_args
  • std::uninitialized_construct_using_allocator
  • std::uses_allocator

더 알아보기 (Learn more)

cppreference