std::allocator_traits<Alloc>::construct
来自 cppreference.cn
定义于头文件 <memory> |
||
template< class T, class... Args > static void construct( Alloc& a, T* p, Args&&... args ); |
(C++11 起) (C++20 起为 constexpr) |
|
若可能,则通过调用 a.construct(p, std::forward<Args>(args)...),在 p 所指向的已分配的未初始化存储中构造 T
类型的对象。
若上述操作不可能(例如 Alloc
没有成员函数 construct()
),则调用
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...) |
(C++20 前) |
std::construct_at(p, std::forward<Args>(args)...) |
(C++20 起) |
目录 |
[编辑] 参数
a | - | 用于构造的分配器 |
p | - | 指向将被构造 T 对象的未初始化存储的指针 |
args... | - | 要传递给 a.construct() 或 放置 new(C++20 前)std::construct_at()(C++20 起) 的构造函数参数 |
[编辑] 返回值
(无)
[编辑] 注意
标准库容器在插入、复制或移动元素时使用此函数。
因为此函数提供了对放置 new 的自动回退,所以自 C++11 起,成员函数 construct()
是一个可选的分配器 (Allocator) 要求。
[编辑] 参阅
分配函数 (函数) | |
(C++20 前) |
在已分配的存储中构造对象 ( std::allocator<T> 的公开成员函数) |
(C++20) |
在给定地址创建对象 (函数模板) |