STL 中的默认分配器具有构造和销毁元素的接口(interface)。
void construct(pointer __p, const _Tp& __val)
void destroy(pointer __p)
但是STL也提供了两种函数来完成同样的事情。这些函数在 STL_construct.h 中定义。
void _Construct(_T1* __p, const _T2& __value)
void _Destroy(_Tp* pointer)
我看到 vector 模板使用 _Construct 和 _Destroy 而不是分配器中定义的接口(interface)。我的问题是为什么我们需要两组函数来完成同一件事?它们有什么区别吗?
最佳答案
_Construct
和_Destroy
函数不是公共(public)接口(interface)的一部分,而是系统上特定标准库版本的实现细节。任何以双下划线或单下划线加大写字母开头的标识符都是保留的,用户不能调用。
这是委托(delegate)分配器的 construct()
的实现选择和destroy()
成员函数到这些非成员函数。顺便说一句,从 C++11 开始,标准容器不再允许直接调用分配器的 construct()
和destroy()
,但必须通过std::allocator_traits<Allocator>
这样做类型特征。
关于c++ - 为什么分配器需要构造和销毁接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799710/