Question

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?

Was it helpful?

Solution

The _Construct and _Destroy functions are not part of the public interface, but are an implementation detail of the particular Standard Library version that you have on your system. Any identifier staring with double underscores or a single underscore and a capital letter, is reserved and not to be called by users.

It's an implementation choice to delegate an allocator's construct() and destroy() member functions to these non-member functions. BTW, since C++11, standard containers are no longer allowed to directly call an allocator's construct() and destroy(), but have to do so through the std::allocator_traits<Allocator> type trait.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top