std::allocator_traits<Alloc>::construct
来自 cppreference.cn
< cpp | memory | allocator traits
定义于头文件 <memory> |
||
template< class T, class... Args > static void construct( Alloc& a, T* p, Args&&... args ); |
(since C++11) (constexpr since C++20) |
|
如果可能,通过调用 a.construct(p, std::forward<Args>(args)...),在 p 指向的已分配但未初始化的存储中构造类型 T
的对象。
如果上述方法不可行(例如,Alloc
没有成员函数 construct()
),则调用
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...) |
(until C++20) |
std::construct_at(p, std::forward<Args>(args)...) |
(since C++20) |
内容 |
[编辑] 参数
a | - | 用于构造的分配器 |
p | - | 指向将在其上构造 T 对象的未初始化存储的指针 |
args... | - | 要传递给 a.construct() 或 placement-new(until C++20)std::construct_at()(since C++20) 的构造函数参数 |
[编辑] 返回值
(无)
[编辑] 注解
标准库容器在插入、复制或移动元素时使用此函数。
由于此函数提供了自动回退到 placement new 的机制,因此成员函数 construct()
是 C++11 之后可选的 Allocator 要求。
[编辑] 参见
分配函数 (函数) | |
(until C++20) |
在已分配的存储中构造对象 ( std::allocator<T> 的公共成员函数) |
(C++20) |
在给定地址创建对象 (函数模板) |