what does (template) rebind<> do?

前端 未结 4 1681
情书的邮戳
情书的邮戳 2020-12-08 20:22

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 s

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 20:40

    The _Alloc template 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 a std::list, the allocator A is meant to allocate objects of type T but the std::list actually needs to allocate objects of some node type. Calling the node type _Ty, the std::list needs to get hold of an allocator for _Ty objects which is using the allocation mechanism provided by A. Using

    typename _A::template rebind<_Ty>::other
    

    specifies the corresponding type. Now, there are a few syntactic annoyances in this declaration:

    1. Since rebind is a member template of _A and _A is a template argument, the rebind becomes a dependent name. To indicate that a dependent name is a template, it needs to be prefixed by template. Without the template keyword the < would be considered to be the less-than operator.
    2. The name other also depends on a template argument, i.e., it is also a dependent name. To indicate that a dependent name is a type, the typename keyword is needed.

提交回复
热议问题