2

The default allocator in stl has interfaces to construct and destroy elements.

void construct(pointer __p, const _Tp& __val)
void destroy(pointer __p)

But stl also provides two kinds of functions to do the same thing. These functions are defined in stl_construct.h.

void _Construct(_T1* __p, const _T2& __value)
void _Destroy(_Tp* pointer)

I see that the vector template uses _Construct and _Destroy rather than the interface defined in allocator. My question is why we need two sets of functions to do the same thing? Do they have any difference?

4

1 回答 1

3

_Construct和函数不是公共接口的_Destroy一部分,而是您系统上特定标准库版本的实现细节。任何以双下划线或单下划线和大写字母开头的标识符都是保留的,不会被用户调用。

construct()将分配器和成员函数委托destroy()给这些非成员函数是一种实现选择。顺便说一句,从 C++11 开始,标准容器不再允许直接调用分配器的construct()and destroy(),而是必须通过std::allocator_traits<Allocator>类型特征来这样做。

于 2013-09-14T08:13:53.627 回答