命名空间
变体
操作

std::allocator_traits<Alloc>::construct

来自 cppreference.cn
 
 
内存管理库
(仅为阐释目的*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
C 库

分配器
内存资源
垃圾回收支持
(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)
未初始化存储
(until C++20*)
(until C++20*)
显式生命周期管理
 
 
定义于头文件 <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> 的公共成员函数) [编辑]
在给定地址创建对象
(函数模板) [编辑]