Question about C ++ template syntax (STL library source code)

I am reading the STL source code right now. Although I understand meat in what I read in stl_list.h, I want to fully understand the following snippet (mostly related to the template syntax, I think).

template

class _List_base {
  ...
  typedef typename _Alloc::template rebind<_List_node<_Tp> >::other _Node_Alloc_type; //(1).

  ...
  typedef _Alloc allocator_type;
  get_allocator() const
  { return allocator_type(*static_cast<
                          const _Node_Alloc_type*>(&this->_M_impl)); }  // (2)
  ...
};

Can someone explain why we need a "pattern" after _Alloc in line (1)? (and giving a full explanation of this line?)

Can someone explain why we can use _Node_Alloc_type for _Alloc in line (2)?

+5
source share
2 answers

template rebind . rebind ( , typename), < .

typename (, , other ).

(.. ) rebind, , . ,

Alloc<T>::rebind<U>::other

,

Alloc<U>

. _M_impl? ?

+12

gcc- std:: list. :

struct _List_impl : public _Node_Alloc_type { ... };
_List_impl _M_impl;

-:

typedef _Alloc allocator_type;
allocator_type
get_allocator() const
{ return allocator_type(*static_cast<const _Node_Alloc_type*>(&this->_M_impl)); }

(1)

node _Tp , , _Tp, node, _Tp (a _List_node<_Tp>).

, std:: list _List_node<_Tp>, _Tp. typedef: U T.

, _Alloc<_List_node<_Tp> > _Alloc<_Tp>.


(2) :

// NOTA BENE
// The stored instance is not actually of "allocator_type"'s
// type.  Instead we rebind the type to
// Allocator<List_node<Tp>>, which according to [20.1.5]/4
// should probably be the same.  List_node<Tp> is not the same
// size as Tp (it two pointers larger), and specializations on
// Tp may go unused because List_node<Tp> is being bound
// instead.
//
// We put this to the test in the constructors and in
// get_allocator, where we use conversions between
// allocator_type and _Node_Alloc_type. The conversion is
// required by table 32 in [20.1.5].

, _Alloc _Node_Alloc_type ++; static_cast , .

+2

All Articles