Namespaces
Variants

std::pmr::polymorphic_allocator<T>:: allocate_object

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)
template < class U >
U * allocate_object ( std:: size_t n = 1 ) ;
(自 C++20 起)

使用底层内存资源为 n 个类型为 U 的对象分配存储空间。

如果 std:: numeric_limits < std:: size_t > :: max ( ) / sizeof ( U ) < n ,则抛出 std::bad_array_new_length ,否则等效于 return static_cast < U * > ( allocate_bytes ( n * sizeof ( U ) , alignof ( U ) ) ) ;

目录

参数

n - 要为分配存储空间的对象数量

返回值

指向已分配存储的指针。

注释

此函数最初为配合完全特化的分配器 std:: pmr :: polymorphic_allocator <> 而引入,但在任何特化场景中均可作为快捷方式使用,以避免从 std:: pmr :: polymorphic_allocator < T > 重新绑定到 std:: pmr :: polymorphic_allocator < U > 的操作。

由于 U 未被推导,调用此函数时必须将其作为模板参数提供。

异常

n > std:: numeric_limits < std:: size_t > :: max ( ) / sizeof ( U ) 时抛出 std::bad_array_new_length ;也可能抛出调用 resource ( ) - > allocate 时产生的任何异常。

参见

从底层资源分配原始对齐内存
(公开成员函数)
(C++20)
分配并构造对象
(公开成员函数)
分配内存
(公开成员函数)
[static]
使用分配器分配未初始化存储
( std::allocator_traits<Alloc> 的公开静态成员函数)
分配内存
( std::pmr::memory_resource 的公开成员函数)