trying to learn more about how the standard library is actually implemented I’m inspecting all containers in visual studio.. Here I see some curious structure:
In some base class of a std::list<> The following typedef is found
typedef typename _Alloc::template rebind<_Ty>::other _Alty;
Where “_Alloc” corresponds with the allocator template argument (and _Ty the contained type). I have trouble finding a good explanation of this “keyword”. Best thing I’ve found so far is that it is part of the allocator interface. Though even cppreference isn’t very good in explaining this.
What does this template rebind<> do? And why is it necessary at that location?
The
_Alloctemplate is used to obtain objects of some type. The container may have an internal need to allocate objects of a different type. For example, when you have astd::list<T, A>, the allocatorAis meant to allocate objects of typeTbut thestd::list<T, A>actually needs to allocate objects of some node type. Calling the node type_Ty, thestd::list<T, A>needs to get hold of an allocator for_Tyobjects which is using the allocation mechanism provided byA. Usingspecifies the corresponding type. Now, there are a few syntactic annoyances in this declaration:
rebindis a member template of_Aand_Ais a template argument, therebindbecomes a dependent name. To indicate that a dependent name is a template, it needs to be prefixed bytemplate. Without thetemplatekeyword the<would be considered to be the less-than operator.otheralso depends on a template argument, i.e., it is also a dependent name. To indicate that a dependent name is a type, thetypenamekeyword is needed.