Skip to content

Search topics & cppreference

Search all study topics, concept maps, and 6,587 C++ and C reference pages.

std::pmr::polymorphic_allocator<T>::new_object

Allocates and constructs an object of type U.

NotebookLM
template< class U, class... CtorArgs >
U* new_object( CtorArgs&&... ctor_args );
(since C++20)

Allocates and constructs an object of type U.

Given alloc is a std::pmr::polymorphic_allocator<T>:

snippet.cpp
U* p = alloc.new_object<U>(std::forward<CtorArgs>(ctor_args)...);

is equivalent to

snippet.cpp
U* p = alloc.allocate_object<U>();
try
{
    alloc.construct(p, std::forward<CtorArgs>(ctor_args)...);
}
catch (...)
{
    alloc.deallocate_object(p);
    throw;
}

Parameters

ctor_args - the arguments to forward to the constructor of U

Return value

A pointer to the allocated and constructed object.

Notes

This function was introduced for use with the fully-specialized allocator std::pmr::polymorphic_allocator<>, but it may be useful in any specialization as a shortcut to avoid having to rebind from std::pmr::polymorphic_allocator<T> to std::pmr::polymorphic_allocator<U>, and having to call allocate, construct, and deallocate individually.

Since U is not deduced, it must be provided as a template argument when calling this function.

Exceptions

May throw any exceptions thrown by the call to allocate_object or the constructor of U.

See also

allocate raw aligned memory from the underlying resource
(public member function)
allocates raw memory suitable for an object or an array
(public member function)
allocate memory
(public member function)
[static]
allocates uninitialized storage using the allocator
(public static member function of std::allocator_traits<Alloc>)
allocates memory
(public member function of std::pmr::memory_resource)

Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.