what does (template) rebind<> do?

前端 未结 4 1694
情书的邮戳
情书的邮戳 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 21:05

    Example in the code of stdc++ : /usr/include/4.8/ext/new_allocator.h

    rebind is defined as a structure member of the allocator class; this structure defines a member other that is defined as an instance of the allocator specialized for a different argument type (the other member defines an allocator class that can creates a different type of objects)

     template
        class new_allocator
        {
        public:
          ...
          template
            struct rebind
            { typedef new_allocator<_Tp1> other; };
    

    When it is used:

      typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
    

    the type of the allocator is referenced as

      typename _Alloc::template rebind<_Tp>::other 
    

    Now the typedef is used to define _Tp_alloc_type - which can then be used as a shorter name for the same thing.

    An example usage is in std::list where the internal list node also needs its allocator, which is redefined from the argument allocator.

提交回复
热议问题